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

php - How to display all orders for a single product showing the most recent first? Woocommerce -

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

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -