Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

Windows 7, python 2.6.6, 2.7

Create directory 'c:\1\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.

like image 851
Sergey Azarkevich Avatar asked Mar 26 '11 11:03

Sergey Azarkevich


1 Answers

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.

like image 191
namey Avatar answered Nov 15 '22 19:11

namey