开发者

python: os.path.isdir return false for directory with dot on end

开发者 https://www.devze.com 2023-02-19 17:45 出处:网络
Windows 7, python 2.6.6, 2.7 Create directory \'c:\\1开发者_C百科\\test.\' Try check if it is dir or file, but it is neither:

Windows 7, python 2.6.6, 2.7

Create directory 'c:\1开发者_C百科\test.'

Try check if it is dir or file, but it is neither:

>>> os.listdir('c:/1')
['test.']
>>> os.path.isdir('c:/1')
True
>>> os.path.exists('c:/1/test.')
False
>>> os.path.isdir('c:/1/test.')
False
>>> os.path.isfile('c:/1/test.')
False

Why directory with . at end not recognized as file system entry at all? But I can obtain it from os.listdir.


As was said in the comments, on Windows, file names that end with a dot, begin/end with spaces, are "aux", etc. etc. etc. - cannot be accessed normally from explorer or from most programming languages.

If you do want to access directories such as "test." from python (or other) code, you can prefix the path with \\?\, for example:

>>> os.path.isdir(r"\\?\c:\1\test.")
True

Note that ".." and "." will not work as usual when using \\?\ paths - windows will try to access an actual file or directory with that name.

0

精彩评论

暂无评论...
验证码 换一张
取 消