This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Native open failed to create a file on windows10 when colon exists in the name
Type: behavior Stage: resolved
Components: IO Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: chenhaoran1991, eryksun, steven.daprano
Priority: normal Keywords:

Created on 2020-11-21 04:37 by chenhaoran1991, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg381524 - (view) Author: Moses (chenhaoran1991) Date: 2020-11-21 04:37
with open('./pdfs/' + pdf_name, 'wb') as fo:
    fo.write(data)

On Windows10, the native 'open' in Python3.7 failed to create a file if the name contains colon, such as 'FocalMix: Semi-Supervised Learning for 3D Medical Image Detection.pdf'.
msg381525 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-11-21 04:48
This is standard Windows behaviour, and goes back to DOS days. Reserved filenames and illegal characters are described here:

https://docs.microsoft.com/en-gb/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN

If the Windows OS and file system does not allow a colon in the file name, there is nothing Python can do to change that.
msg381526 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-11-21 06:43
> 'FocalMix: Semi-Supervised Learning for 3D Medical Image Detection.pdf'

In a filesystem that supports file streams [1], such as NTFS or ReFS, the above name refers to a $DATA stream named " Semi-Supervised Learning for 3D Medical Image Detection.pdf" in a file name "FocalMix".

Filesystems that do not support file streams may allow colon in filenames, regardless of its reserved status in the API. FAT filesystems (e.g. FAT32, exFAT) disallow colon in filenames. But the VirtualBox shared-folder filesystem (redirector) allows it. Even if colon is allowed by a particular filesystem, I strongly advise that you never use it in filenames, considering the problems with moving the file to an NTFS drive.

You also need to be vigilant about using DOS device names (e.g. "con", "nul:", "com1.txt", etc) in filenames. Creating or opening them may succeed, but probably not in the way that you expect. For example, if you open r"C:\Temp\nul.txt" for writing, you're actually opening the r"\\.\NUL" device, and all of the data written is simply thrown away without error.

---

[1] https://docs.microsoft.com/en-us/windows/win32/fileio/file-streams
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86587
2020-11-21 06:43:47eryksunsetnosy: + eryksun
messages: + msg381526
2020-11-21 04:48:27steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg381525

resolution: not a bug
stage: resolved
2020-11-21 04:37:46chenhaoran1991create