Skip to content

Spaces at the end of file and directory names are silently ignored on windows #115104

@timhoffm

Description

@timhoffm

Bug report

Bug description:

>>> import os
>>> os.path.exists("test")
False
>>> os.path.exists("test ")
False
>>> os.mkdir("test ")
>>> os.path.exists("test")
True
>>> os.path.exists("test ")
True

The explorer also shows the created directory as test, i.e. without trailing spaces.

The win32 docs say about trailing spaces

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not.

One could discuss whether this is user-responsibility, but silently dropping trailing spaces feels wrong. Either create the directory (though not recommended), or raise if there is a space at the end.

Note: Behaviour gets even more weird when combining trailing spaces with alternate data streams, e.g. "test : foo" (matplotlib/matplotlib#27748), which brought me here. But before going into this more difficult discussion, I'd like to learn the general position on handling trailings spaces in windows filenames.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

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