tell me how to make a condition: if the file does not exist | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

tell me how to make a condition: if the file does not exist

tell me how to make a condition: if the file does not exist

14th Aug 2021, 8:59 AM
Alisa Zaiceva
4 Answers
+ 4
Alisa Zaiceva , put the part of the code that is trying to open a file in a try... except... construct. [Edited]: using to check if a file exists by using os.path or pathlib module does physically check if the file exists. that is ok - no doubts. but there may be a whole bunch of issues that can happen when accessing or during using the file and make the app crashing: (* = handled by doing the mentioned check) - file not found * - no such directory * - file in use or blocked - no permission to access - bad file descriptor: using wrong mode - no space left on the device - file is read-only - resource temporarily unavailable - network dropped connection - connection timed out - connection refused - input/output error - .....
14th Aug 2021, 10:33 AM
Lothar
Lothar - avatar
14th Aug 2021, 9:10 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
Google something like "Python file exists" for examples and tutorials https://www.pythontutorial.net/python-basics/python-check-if-file-exists/
14th Aug 2021, 9:13 AM
Lisa
Lisa - avatar
0
Alisa Zaiceva How about doing this in cmd? :- if exist ... ?
14th Aug 2021, 5:24 PM
Calvin Thomas
Calvin Thomas - avatar