os.path.abspath超级奇怪的错误 [英] A super strange bug of os.path.abspath

查看:125
本文介绍了os.path.abspath超级奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python 2.6(64bit,win7,ActivePython),
当我打电话时:
os.path.abspath('D:/ PROJECTS / SuiShouBei / www / ssb它返回:

<$ c $ / code c>'\\\\\\\\''



到目前为止,我对其他路径没有任何问题。

任何人都有同样的问题?

有人可以告诉我为什么?

解决方案

我可以在Python 2.6,2.7,3.1和3.2中重现这个问题。



这个行为的原因是 CON 是一个非法文件名(尝试 os.path.abspath('D:/PROJECTS/SuiShouBei/www/ssb/static/voices/en/mp3/cont.mp3')
<$ p

$ p> < (小于)
> (大于)
:(冒号)
(双引号)
/(正斜杠)
\(反斜杠)
|(竖线或竖线)
?(问号)
*(星号)

以下保留的设备名称为文件名(带或不带扩展名):

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

正如slowdog所注意到的,它在与上面相同的MSDN文档 \\\\\\\ 是正确的直接访问这样一个设备名称的方法。

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?

解决方案

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.

这篇关于os.path.abspath超级奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆