View, append, write, or delete a file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I am just wondering if I need to refactor this code into smaller functions, or if it is OK as it is. I know that code should be as clean as possible, but I don't see a need to declare a new function every time I have more than 5 lines of code. Anyways please tell me what you think.



The code itself is used for file handling. It allows a user to create, append, read, or delete files. This is all based on user input.



def option_two():
#####################################################################
# Option 2, File Creation/Editing: #
# Asks the user what they would like to do #
# They can view, append, write, or delete a file #
#####################################################################

path_exists = bool

try:
USER_INPUT = int(input("""
What would you like to do?n
1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """))


if USER_INPUT == 1:
#View a file
user_message(1)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
open_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
path_exists = check_path(USER_INPUT, path_exists)
if path_exists:
open_file(USER_INPUT)
else:
option_two()
else:
print("Enter y or n only")
option_two()
return

elif USER_INPUT == 2:
#append to a file
user_message(2)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
append_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()

elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
if path_exists:
append_file(USER_INPUT)
else:
option_two()

else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 3:
#Write a new file
user_message(3)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
write_file("//home//alpha//Desktop//0".format(USER_INPUT))
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
write_file(USER_INPUT)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 4:
#Delete a file
user_message(4)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
delete_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
delete_path(USER_INPUT, path_exists)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 5:
user_message(5)
print("Moving back to main")
return

except(ValueError):
print("Invalid input, try againnn")
option_two()









share|improve this question









New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. The current title states your concerns about the code; it needs an edit to simply state the task; see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
    – Toby Speight
    2 hours ago






  • 1




    @TobySpeight there are an abundance of comments in the code that state exactly what it is doing. My question was clear, should it be refactored into smaller chunks. I am looking for constructive criticism.
    – Robotica
    2 hours ago
















up vote
3
down vote

favorite












I am just wondering if I need to refactor this code into smaller functions, or if it is OK as it is. I know that code should be as clean as possible, but I don't see a need to declare a new function every time I have more than 5 lines of code. Anyways please tell me what you think.



The code itself is used for file handling. It allows a user to create, append, read, or delete files. This is all based on user input.



def option_two():
#####################################################################
# Option 2, File Creation/Editing: #
# Asks the user what they would like to do #
# They can view, append, write, or delete a file #
#####################################################################

path_exists = bool

try:
USER_INPUT = int(input("""
What would you like to do?n
1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """))


if USER_INPUT == 1:
#View a file
user_message(1)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
open_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
path_exists = check_path(USER_INPUT, path_exists)
if path_exists:
open_file(USER_INPUT)
else:
option_two()
else:
print("Enter y or n only")
option_two()
return

elif USER_INPUT == 2:
#append to a file
user_message(2)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
append_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()

elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
if path_exists:
append_file(USER_INPUT)
else:
option_two()

else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 3:
#Write a new file
user_message(3)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
write_file("//home//alpha//Desktop//0".format(USER_INPUT))
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
write_file(USER_INPUT)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 4:
#Delete a file
user_message(4)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
delete_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
delete_path(USER_INPUT, path_exists)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 5:
user_message(5)
print("Moving back to main")
return

except(ValueError):
print("Invalid input, try againnn")
option_two()









share|improve this question









New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. The current title states your concerns about the code; it needs an edit to simply state the task; see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
    – Toby Speight
    2 hours ago






  • 1




    @TobySpeight there are an abundance of comments in the code that state exactly what it is doing. My question was clear, should it be refactored into smaller chunks. I am looking for constructive criticism.
    – Robotica
    2 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I am just wondering if I need to refactor this code into smaller functions, or if it is OK as it is. I know that code should be as clean as possible, but I don't see a need to declare a new function every time I have more than 5 lines of code. Anyways please tell me what you think.



The code itself is used for file handling. It allows a user to create, append, read, or delete files. This is all based on user input.



def option_two():
#####################################################################
# Option 2, File Creation/Editing: #
# Asks the user what they would like to do #
# They can view, append, write, or delete a file #
#####################################################################

path_exists = bool

try:
USER_INPUT = int(input("""
What would you like to do?n
1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """))


if USER_INPUT == 1:
#View a file
user_message(1)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
open_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
path_exists = check_path(USER_INPUT, path_exists)
if path_exists:
open_file(USER_INPUT)
else:
option_two()
else:
print("Enter y or n only")
option_two()
return

elif USER_INPUT == 2:
#append to a file
user_message(2)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
append_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()

elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
if path_exists:
append_file(USER_INPUT)
else:
option_two()

else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 3:
#Write a new file
user_message(3)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
write_file("//home//alpha//Desktop//0".format(USER_INPUT))
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
write_file(USER_INPUT)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 4:
#Delete a file
user_message(4)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
delete_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
delete_path(USER_INPUT, path_exists)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 5:
user_message(5)
print("Moving back to main")
return

except(ValueError):
print("Invalid input, try againnn")
option_two()









share|improve this question









New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am just wondering if I need to refactor this code into smaller functions, or if it is OK as it is. I know that code should be as clean as possible, but I don't see a need to declare a new function every time I have more than 5 lines of code. Anyways please tell me what you think.



The code itself is used for file handling. It allows a user to create, append, read, or delete files. This is all based on user input.



def option_two():
#####################################################################
# Option 2, File Creation/Editing: #
# Asks the user what they would like to do #
# They can view, append, write, or delete a file #
#####################################################################

path_exists = bool

try:
USER_INPUT = int(input("""
What would you like to do?n
1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """))


if USER_INPUT == 1:
#View a file
user_message(1)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
open_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
path_exists = check_path(USER_INPUT, path_exists)
if path_exists:
open_file(USER_INPUT)
else:
option_two()
else:
print("Enter y or n only")
option_two()
return

elif USER_INPUT == 2:
#append to a file
user_message(2)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
path_exists = check_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
if path_exists:
append_file("//home//alpha//Desktop//0".format(USER_INPUT))
else:
option_two()

elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
if path_exists:
append_file(USER_INPUT)
else:
option_two()

else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 3:
#Write a new file
user_message(3)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
write_file("//home//alpha//Desktop//0".format(USER_INPUT))
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
write_file(USER_INPUT)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 4:
#Delete a file
user_message(4)

USER_INPUT = input("Is this file located on the desktop? y/n")
if USER_INPUT == "y":
USER_INPUT = input("What is the name of the file?")
delete_path("//home//alpha//Desktop//0".format(USER_INPUT), path_exists)
elif USER_INPUT == "n":
USER_INPUT = input("Enter the full path for the file")
delete_path(USER_INPUT, path_exists)
else:
print("Enter a y or n only")
option_two()
return

elif USER_INPUT == 5:
user_message(5)
print("Moving back to main")
return

except(ValueError):
print("Invalid input, try againnn")
option_two()






python python-3.x file






share|improve this question









New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Graipher

21.2k43183




21.2k43183






New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Robotica

235




235




New contributor




Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Robotica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. The current title states your concerns about the code; it needs an edit to simply state the task; see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
    – Toby Speight
    2 hours ago






  • 1




    @TobySpeight there are an abundance of comments in the code that state exactly what it is doing. My question was clear, should it be refactored into smaller chunks. I am looking for constructive criticism.
    – Robotica
    2 hours ago
















  • This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. The current title states your concerns about the code; it needs an edit to simply state the task; see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
    – Toby Speight
    2 hours ago






  • 1




    @TobySpeight there are an abundance of comments in the code that state exactly what it is doing. My question was clear, should it be refactored into smaller chunks. I am looking for constructive criticism.
    – Robotica
    2 hours ago















This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. The current title states your concerns about the code; it needs an edit to simply state the task; see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
2 hours ago




This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. The current title states your concerns about the code; it needs an edit to simply state the task; see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
2 hours ago




1




1




@TobySpeight there are an abundance of comments in the code that state exactly what it is doing. My question was clear, should it be refactored into smaller chunks. I am looking for constructive criticism.
– Robotica
2 hours ago




@TobySpeight there are an abundance of comments in the code that state exactly what it is doing. My question was clear, should it be refactored into smaller chunks. I am looking for constructive criticism.
– Robotica
2 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










In my opinion this can be greatly simplified. First, there is no need for this to be a recursive function (which are seldom the best choice in Python due to the maximum recursion depth). Just make it an infinite loop.



Second, all of your options are almost the same. You ask if the file is on the desktop, then for the file name/path and then do something with that path. So, just define a function for that first part:



USERNAME = "alpha"

def ask_file_name():
user_input = input("Is this file located on the desktop? y/n")
if user_input.lower() == "y":
path = "/home//Desktop/"
return path.format(USERNAME, input("What is the name of the file?"))
return input("Enter the full path for the file")


Then your main function becomes rather short:



MENU = """
What would you like to do?

1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """

def option_two():
"""Option 2, File Creation/Editing:

Asks the user what they would like to do
They can view, append, write, or delete a file
"""

while True:
try:
user_input = int(input(MENU))
except ValueError:
print("Invalid input, try againnn")
continue
user_message(user_input)
path = ask_file_name()

if user_input == 1:
#View a file
if check_path(path):
open_file(path)
else:
continue
elif user_input == 2:
#append to a file
if check_path(path):
append_file(path)
else:
continue
elif user_input == 3:
#Write a new file
write_file(path)
elif user_input == 4:
#Delete a file
delete_path(path)
print("Moving back to main")
return


Note that check_path should work without having a boolean variable passed in and delete_path should probably just call check_path internally (or use try...except).



I also changed your comment under the function definition to conform to Python's docstring convention.






share|improve this answer






















  • Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
    – Robotica
    37 mins ago










  • @Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
    – Graipher
    35 mins ago










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Robotica is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f203693%2fview-append-write-or-delete-a-file%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










In my opinion this can be greatly simplified. First, there is no need for this to be a recursive function (which are seldom the best choice in Python due to the maximum recursion depth). Just make it an infinite loop.



Second, all of your options are almost the same. You ask if the file is on the desktop, then for the file name/path and then do something with that path. So, just define a function for that first part:



USERNAME = "alpha"

def ask_file_name():
user_input = input("Is this file located on the desktop? y/n")
if user_input.lower() == "y":
path = "/home//Desktop/"
return path.format(USERNAME, input("What is the name of the file?"))
return input("Enter the full path for the file")


Then your main function becomes rather short:



MENU = """
What would you like to do?

1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """

def option_two():
"""Option 2, File Creation/Editing:

Asks the user what they would like to do
They can view, append, write, or delete a file
"""

while True:
try:
user_input = int(input(MENU))
except ValueError:
print("Invalid input, try againnn")
continue
user_message(user_input)
path = ask_file_name()

if user_input == 1:
#View a file
if check_path(path):
open_file(path)
else:
continue
elif user_input == 2:
#append to a file
if check_path(path):
append_file(path)
else:
continue
elif user_input == 3:
#Write a new file
write_file(path)
elif user_input == 4:
#Delete a file
delete_path(path)
print("Moving back to main")
return


Note that check_path should work without having a boolean variable passed in and delete_path should probably just call check_path internally (or use try...except).



I also changed your comment under the function definition to conform to Python's docstring convention.






share|improve this answer






















  • Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
    – Robotica
    37 mins ago










  • @Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
    – Graipher
    35 mins ago














up vote
3
down vote



accepted










In my opinion this can be greatly simplified. First, there is no need for this to be a recursive function (which are seldom the best choice in Python due to the maximum recursion depth). Just make it an infinite loop.



Second, all of your options are almost the same. You ask if the file is on the desktop, then for the file name/path and then do something with that path. So, just define a function for that first part:



USERNAME = "alpha"

def ask_file_name():
user_input = input("Is this file located on the desktop? y/n")
if user_input.lower() == "y":
path = "/home//Desktop/"
return path.format(USERNAME, input("What is the name of the file?"))
return input("Enter the full path for the file")


Then your main function becomes rather short:



MENU = """
What would you like to do?

1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """

def option_two():
"""Option 2, File Creation/Editing:

Asks the user what they would like to do
They can view, append, write, or delete a file
"""

while True:
try:
user_input = int(input(MENU))
except ValueError:
print("Invalid input, try againnn")
continue
user_message(user_input)
path = ask_file_name()

if user_input == 1:
#View a file
if check_path(path):
open_file(path)
else:
continue
elif user_input == 2:
#append to a file
if check_path(path):
append_file(path)
else:
continue
elif user_input == 3:
#Write a new file
write_file(path)
elif user_input == 4:
#Delete a file
delete_path(path)
print("Moving back to main")
return


Note that check_path should work without having a boolean variable passed in and delete_path should probably just call check_path internally (or use try...except).



I also changed your comment under the function definition to conform to Python's docstring convention.






share|improve this answer






















  • Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
    – Robotica
    37 mins ago










  • @Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
    – Graipher
    35 mins ago












up vote
3
down vote



accepted







up vote
3
down vote



accepted






In my opinion this can be greatly simplified. First, there is no need for this to be a recursive function (which are seldom the best choice in Python due to the maximum recursion depth). Just make it an infinite loop.



Second, all of your options are almost the same. You ask if the file is on the desktop, then for the file name/path and then do something with that path. So, just define a function for that first part:



USERNAME = "alpha"

def ask_file_name():
user_input = input("Is this file located on the desktop? y/n")
if user_input.lower() == "y":
path = "/home//Desktop/"
return path.format(USERNAME, input("What is the name of the file?"))
return input("Enter the full path for the file")


Then your main function becomes rather short:



MENU = """
What would you like to do?

1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """

def option_two():
"""Option 2, File Creation/Editing:

Asks the user what they would like to do
They can view, append, write, or delete a file
"""

while True:
try:
user_input = int(input(MENU))
except ValueError:
print("Invalid input, try againnn")
continue
user_message(user_input)
path = ask_file_name()

if user_input == 1:
#View a file
if check_path(path):
open_file(path)
else:
continue
elif user_input == 2:
#append to a file
if check_path(path):
append_file(path)
else:
continue
elif user_input == 3:
#Write a new file
write_file(path)
elif user_input == 4:
#Delete a file
delete_path(path)
print("Moving back to main")
return


Note that check_path should work without having a boolean variable passed in and delete_path should probably just call check_path internally (or use try...except).



I also changed your comment under the function definition to conform to Python's docstring convention.






share|improve this answer














In my opinion this can be greatly simplified. First, there is no need for this to be a recursive function (which are seldom the best choice in Python due to the maximum recursion depth). Just make it an infinite loop.



Second, all of your options are almost the same. You ask if the file is on the desktop, then for the file name/path and then do something with that path. So, just define a function for that first part:



USERNAME = "alpha"

def ask_file_name():
user_input = input("Is this file located on the desktop? y/n")
if user_input.lower() == "y":
path = "/home//Desktop/"
return path.format(USERNAME, input("What is the name of the file?"))
return input("Enter the full path for the file")


Then your main function becomes rather short:



MENU = """
What would you like to do?

1)Read a file
2)Append to an existing file
3)Write to a new file
4)Delete a file
5)Go back/n
>>> """

def option_two():
"""Option 2, File Creation/Editing:

Asks the user what they would like to do
They can view, append, write, or delete a file
"""

while True:
try:
user_input = int(input(MENU))
except ValueError:
print("Invalid input, try againnn")
continue
user_message(user_input)
path = ask_file_name()

if user_input == 1:
#View a file
if check_path(path):
open_file(path)
else:
continue
elif user_input == 2:
#append to a file
if check_path(path):
append_file(path)
else:
continue
elif user_input == 3:
#Write a new file
write_file(path)
elif user_input == 4:
#Delete a file
delete_path(path)
print("Moving back to main")
return


Note that check_path should work without having a boolean variable passed in and delete_path should probably just call check_path internally (or use try...except).



I also changed your comment under the function definition to conform to Python's docstring convention.







share|improve this answer














share|improve this answer



share|improve this answer








edited 38 mins ago

























answered 46 mins ago









Graipher

21.2k43183




21.2k43183











  • Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
    – Robotica
    37 mins ago










  • @Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
    – Graipher
    35 mins ago
















  • Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
    – Robotica
    37 mins ago










  • @Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
    – Graipher
    35 mins ago















Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
– Robotica
37 mins ago




Thanks for the good info. Idk why I didn't think about putting it in a loop. This isn't exactly related but I've never heard of a max recursion depth?
– Robotica
37 mins ago












@Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
– Graipher
35 mins ago




@Robotica: Have a look here: stackoverflow.com/questions/3323001/… By default CPython stops after 1000 levels of recursion (which will be a bit hard to reach in a menu, but is not impossible...)
– Graipher
35 mins ago










Robotica is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















Robotica is a new contributor. Be nice, and check out our Code of Conduct.












Robotica is a new contributor. Be nice, and check out our Code of Conduct.











Robotica is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f203693%2fview-append-write-or-delete-a-file%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke