You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
os.path.exists() returns False for certain file name #74013
I have two files in two different folders, both on desktop.
If I try using os.path.exists() on both of them, it returns True for one file and False for the other file.
After renaming file that I got False from, I get returned True and when I rename it back, it returns False again.
File name causing the problem is "testni.wav", I assume it's anything starting with "test", but I'm sure you guys will know better. Thank you
In a string literal, '\t' represents a tab character (ordinal 9). Windows filenames cannot contain control characters 1, i.e. ordinals 1-31. For path literals I recommend using forward slashes and normalizing via os.path.normpath, e.g. filepath = os.path.normpath('C:/Users/mavri/Desktop/proba/testni.wav').
FYI, the way exists() is written handles any OSError as False:
Activity
MarkoMavrinac commentedon Mar 16, 2017
I have two files in two different folders, both on desktop.
If I try using os.path.exists() on both of them, it returns True for one file and False for the other file.
After renaming file that I got False from, I get returned True and when I rename it back, it returns False again.
File name causing the problem is "testni.wav", I assume it's anything starting with "test", but I'm sure you guys will know better. Thank you
Detailed description with screenshots can be seen here: http://stackoverflow.com/questions/42834408/os-path-exists-returning-false-for-one-and-true-for-another-file-both-files-e
MarkoMavrinac commentedon Mar 16, 2017
I am sorry, I didn't realize \t affected how the path was recognized.
eryksun commentedon Mar 16, 2017
In a string literal, '\t' represents a tab character (ordinal 9). Windows filenames cannot contain control characters 1, i.e. ordinals 1-31. For path literals I recommend using forward slashes and normalizing via os.path.normpath, e.g. filepath = os.path.normpath('C:/Users/mavri/Desktop/proba/testni.wav').
FYI, the way exists() is written handles any OSError as False:
In your case, stat() fails with ERROR_INVALID_NAME (123):
>>> os.stat('\testni.wav') Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '\testni.wav'