Title: os.mkdir creates invalid folder when path string has trailing space and slash
Type: behavior Stage: resolved
Components: IO, Windows Versions: Python 3.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: hollyroberts, paul.moore, steve.dower, steven.daprano, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2019-07-05 14:01 by hollyroberts, last changed 2019-07-05 18:21 by steve.dower. This issue is now closed.

Messages (3)
msg347346 - (view) Author: Holly (hollyroberts) Date: 2019-07-05 14:01
If we use os.mkdir to create a folder as follows:
    os.mkdir("C:/test /")
Then we successfully create a folder with name "test ". This folder can be accessed normally, but it cannot be renamed or deleted. Instead we get the following error message:
    Could not find this item
    This is no longer located in C:\. Verify the item's location and try again.
I used Python 3.7.3 (64 bit) and Windows 1809

Obviously this is down to how windows doesn't handle paths particularly nicely, so it's not really python's fault. However even if it's decided that python shouldn't handle this then it would be better for an exception to be thrown rather than creating a corrupt folder.

Note: We can delete the folder by using the Long UNC path, eg. rd "\\?\C:\test "
msg347354 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-07-05 14:24
> it would be better for an exception to be thrown rather than creating a corrupt folder.

I am not convinced that Python should refuse to create a perfectly valid directory because some components of Windows sometimes have trouble resolving the pathname under some circumstances.

Microsoft say:

"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."

but Python is talking to the file system, not the shell or Windows Explorer.

This is a common problem, with numerous solutions:

It's apparently quite common in shared environments where (for example) Mac users may copy files ending with a space onto Windows drives.
msg347366 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-07-05 18:21
Yeah, I'd rather not do this level of validation in Python.

If Windows decides to reject or automatically correct this, fine, but Python is just being a thin layer over the operating system here. We shouldn't try and prevent things that the OS allows.
Date User Action Args
2019-07-05 18:21:44steve.dowersetstatus: open -> closed
resolution: rejected
messages: + msg347366

stage: resolved
2019-07-05 14:24:08steven.dapranosetnosy: + paul.moore, tim.golden, steven.daprano, zach.ware, steve.dower
messages: + msg347354
components: + Windows
2019-07-05 14:01:48hollyrobertscreate