2019-06-13
Python Unicode filename \x01 on Windows
源自: stackoverflow
问题

I have a Python script which generates a bunch of files with various Unicode filenames for testing purposes. It works fine on OS X and Linux environments. On Windows (and more importantly AppVeyor) it fails to create some files, complaining about an invalid filename. Here is a very minimal version:

with open("\x01", "w") as f:
  f.write("foo")

The error I get is:

Traceback (most recent call last):
  File "genTestRes.py", line 51, in <module>
    os.mkdir(os.path.join(TESTDIR, data))
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'test-res\\\x01'

I can reproduce the CI failure locally on a Windows machine with the minimal script above, on an NTFS disk, with Python 2.7. None of these helped:

  • adding # -*- coding: utf-8 -*- at the beginning
  • running chcp 65001 before the command
  • using the Unicode prefix (u"\x01")

According to Wikipedia, NTFS should support at least

In Win32 namespace: any UTF-16 code unit (case-insensitive) except /:*"?<>| as well as NUL

What could be causing this? U+0001 should be a perfectly fine, if unprintable, codepoint. Is Wikipedia simply wrong here?

答案
1

In Windows, NTFS filenames cannot contain the ASCII control characters 0x01 - 0x1F. See Naming Files, Paths, and Namespaces. This rule applies to all of Microsoft's filesystems, though not necessarily to third-party filesystems. For example, the VirtualBox shared-folder filesystem allows control characters, colon, and pipe in filenames.

Python Unicode filename \x01 on Windows
查看原网页