Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.path.exists() returns False for certain file name #74013

Closed
MarkoMavrinac mannequin opened this issue Mar 16, 2017 · 3 comments
Closed

os.path.exists() returns False for certain file name #74013

MarkoMavrinac mannequin opened this issue Mar 16, 2017 · 3 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@MarkoMavrinac
Copy link
Mannequin

MarkoMavrinac mannequin commented Mar 16, 2017

BPO 29827
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-03-16.15:04:59.216>
created_at = <Date 2017-03-16.14:27:55.075>
labels = ['type-bug', 'invalid', 'OS-windows']
title = 'os.path.exists() returns False for certain file name'
updated_at = <Date 2017-03-16.15:04:59.213>
user = 'https://bugs.python.org/MarkoMavrinac'

bugs.python.org fields:

activity = <Date 2017-03-16.15:04:59.213>
actor = 'eryksun'
assignee = 'none'
closed = True
closed_date = <Date 2017-03-16.15:04:59.216>
closer = 'eryksun'
components = ['Windows']
creation = <Date 2017-03-16.14:27:55.075>
creator = 'Marko Mavrinac'
dependencies = []
files = []
hgrepos = []
issue_num = 29827
keywords = []
message_count = 3.0
messages = ['289717', '289718', '289719']
nosy_count = 6.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'Marko Mavrinac']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue29827'
versions = ['Python 2.7']

@MarkoMavrinac
Copy link
Mannequin Author

MarkoMavrinac mannequin commented 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 MarkoMavrinac mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Mar 16, 2017
@MarkoMavrinac
Copy link
Mannequin Author

MarkoMavrinac mannequin commented Mar 16, 2017

I am sorry, I didn't realize \t affected how the path was recognized.

@eryksun
Copy link
Contributor

eryksun commented 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:

    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'

@eryksun eryksun closed this as completed Mar 16, 2017
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant