Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A super strange bug of os.path.abspath

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?

like image 300
Vergil Lu Avatar asked Aug 08 '11 15:08

Vergil Lu


People also ask

What is this os path abspath(__ file__?

According to docs os. path. abspath() returns a normalized absolutized version of the pathname path which may sound fancy but it simply means that this method returns the pathname to the path passed as a parameter to this function.

What is path Abspath?

abspath (path) Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)) . Changed in version 3.6: Accepts a path-like object.


1 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.

like image 97
Tim Pietzcker Avatar answered Oct 18 '22 02:10

Tim Pietzcker