This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.path.exists() returns False for certain file name
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Marko Mavrinac, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-03-16 14:27 by Marko Mavrinac, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg289717 - (view) Author: Marko Mavrinac (Marko Mavrinac) Date: 2017-03-16 14:27
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
msg289718 - (view) Author: Marko Mavrinac (Marko Mavrinac) Date: 2017-03-16 14:57
I am sorry, I didn't realize \t affected how the path was recognized.
msg289719 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-03-16 15:04
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:

    def exists(path):
        try:
            os.stat(path)
        except OSError:
            return False
        return True

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'

[1]: https://msdn.microsoft.com/en-us/library/aa365247#naming_conventions
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74013
2017-03-16 15:04:59eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg289719

resolution: not a bug
stage: resolved
2017-03-16 14:57:03Marko Mavrinacsetmessages: + msg289718
2017-03-16 14:27:55Marko Mavrinaccreate