python - How to keep asking the user for a file -


this question has answer here:

new programmer here, , let me start off code have.

try:     f = input("please type in path file , press 'enter'")     file = open(f,'r') except filenotfounderror:     f = input("file not found please try again.") 

what i'm trying accomplish, if user enters wrong file, keep asking user try again. maybe shouldn't using try/except?

embed statement inside while loop. break if file opened successfully.

while true:     try:         f = input("please type in path file , press 'enter'")         file = open(f, 'r')         break     except filenotfounderror:         print('file not found') 

note: may need handle other exceptions ioerror (even though, there file, may not possible open - because of permsssion, wrong file type, ..)


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -