A super strange bug of os.path.abspath

Question:

On My Python 2.6 ( 64bit, win7, ActivePython ),
when i call:
os.path.abspath('D:/PROJECTS/SuiShouBei/www/ssb/static/voices/en/mp3/con.mp3')

It returns:
'\\.\con'

I have no problem with other paths so far.

Anyone has the same issue?
Can someone please tell me why?

Asked By: Vergil Lu

||

Answers:

I can reproduce this in Python 2.6, 2.7, 3.1 and 3.2.

The reason for this behavior is the fact that CON is an illegal filename in Windows (try os.path.abspath('D:/PROJECTS/SuiShouBei/www/ssb/static/voices/en/mp3/cont.mp3') and see that everything works fine).

So take care that your filenames don’t contain

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
 (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

Also do not use the following reserved device names for the name of a file (with or without extension):

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, 
LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. 

As noticed by slowdog, it is mentioned in the same MSDN document as above that \.CON is the correct way to access such a device name directly.

Answered By: Tim Pietzcker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.