classification
Title: os.mkdir on Windows silently strips trailing blanks from directory names
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, steve.dower, tegavu, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2014-10-27 18:04 by tegavu, last changed 2017-03-24 16:22 by serhiy.storchaka. This issue is now closed.

Messages (5)
msg230082 - (view) Author: (tegavu) Date: 2014-10-27 18:04
Windows does not like/permit folders with spaces in the beginning or folders and files with a tailing space character, as this will cause problems.
The python functions for os.mkdir will solve this by eliminating the blanks automatically.
But os.path.join() will give wrong results.

Example:

#Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23)
import os
dir1 = "c:\\"
dir2 = "test   "
file = "test.txt"     

os.mkdir( os.path.join(dir1, dir2) )    
    # this will correctly create c:\test\
f = open( os.path.join(dir1, dir2, file) ,"wb")
    # this will fail with 'FileNotFoundError: [Errno 2] No such file or directory: 'c:\\test     \\test.txt''
print("__" + os.path.join(dir1, dir2, file) + "__")   
    # this will incorrectly show 'c:\test     \test.txt'
    # or if you chose to also have spaces at the end of "test.txt     " will show them
msg230097 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-27 19:28
I would classify that as a bug in mkdir (it should raise an error).  The blank-elimination is almost certainly being done at the OS layer (windows) rather than the Python layer, though, and I doubt it is something we can fix at the Python layer for backward compatibility reasons.  (If we can, it would only be in 3.5, so I'm changing the version).

It is definitely *not* a bug in join.

Probably this issue should be closed as not a bug.
msg230100 - (view) Author: (tegavu) Date: 2014-10-27 19:58
Well then the bug is also for example in the open() to create a file, beause it will also remove tailing spaces (files can have spaces in the front, while folders cannot have spces on either side).

Although I still think an API to create a path should warn if this path cannot be valid.
msg230101 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2014-10-27 20:14
Paths are processed by the Windows API. This processing includes (at least):

* resolve to current directory
* convert forward slashes to backslashes
* remove adjacent backslashes
* trim trailing dots and spaces from names
* resolve short path names

This processing can be skipped with the '\\?\' prefix, but in general it shouldn't be.

I would be okay with seeing this processing performed by pathlib so that str(Path("C:\\") / "test    ") == "C:\\test" (or maybe as part of .resolve() - there's another issue where we started discussing this), but I would be really hesitant to change ntpath to do it or detect it.
msg230103 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-27 20:22
Yeah, to os.path, paths are just strings, and there's no reference made to the actual file system.  The blanks are valid on unix, for example (unless you pass them to the shell without escaping them).  As Steve indicated, parts of Pathlib might make a different decision about that, but os.path...can't really, and still remain a mostly-portable API (not to mention backward compatibility).

Or, to look at this another way, Python is giving you exactly the errors that you would get from Windows itself if you passed it the strings you are passing it, and no more.  Python is faithfully constructing those strings according to *string* rules, and it is Windows that is transforming *some* of them into other strings.
History
Date User Action Args
2017-03-24 16:22:12serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2014-10-27 20:22:58r.david.murraysetmessages: + msg230103
2014-10-27 20:14:25steve.dowersetmessages: + msg230101
2014-10-27 19:58:13tegavusetmessages: + msg230100
2014-10-27 19:28:09r.david.murraysettitle: os.path.join on Windows creates invalid paths with spaces -> os.mkdir on Windows silently strips trailing blanks from directory names
nosy: + tim.golden, r.david.murray, zach.ware, steve.dower

messages: + msg230097

versions: + Python 3.5, - Python 3.4
components: + Windows
2014-10-27 18:04:12tegavucreate