exception handling - Handle error in python -


i'm new python , want know best way handle following error:

downloaded = raw_input("introduce file name: ").strip() f, metadata = client.get_file_and_metadata('/'+downloaded) #print 'metadata: ', metadata['mime_type'] out = open(downloaded, 'wb') out.write(f.read()) out.close() 

if set wrong name error:

dropbox.rest.errorresponse: [404] u'file not found' 

i write function check if file exists want know if can handle in better way.

i assume want try open file , if fail, prompt user try again?

filefound = false while not filefound:      downloaded = raw_input("introduce file name: ").strip()     try:         f, metadata = client.get_file_and_metadata('/'+downloaded)         #print 'metadata: ', metadata['mime_type']         filefound = true     except dropbox.rest.errorresponse e:         if e.status == 404:             print("file " + downloaded + " not found, please try again")             filefound = false         else:             raise e  out = open(downloaded, 'wb') out.write(f.read()) out.close() 

as pointed out @superbiasedman , @geckon, trying call client.get_file_and_metadata and, if fails exception dropbox.rest.errorresponse, handling in way. in case error handling code checks if error 404 (file missing) , tells user try different file. filefound = false, results in prompt user again. if error not file missing, error raised , code stops.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -