Skip to content

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

Closed

Description

@MarkoMavrinac
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']

Activity

  1. MarkoMavrinac commented on Mar 16, 2017

    MarkoMavrinacmannequin
    MannequinAuthor

    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

  2. MarkoMavrinac commented on Mar 16, 2017

    MarkoMavrinacmannequin
    MannequinAuthor

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

  3. eryksun commented on Mar 16, 2017

    @eryksun
    Contributor

    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'
  4. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions