os.path.exists在Python CLI上不能正常工作



我有Python 2.5。x安装在我的Windows 7电脑上。

os.path.exists('C:')              # returns True
os.path.exists('C:Users')        # returns True
os.path.exists('C:Usersalpha')  # returns False, when ALPHA is a user on my machine

我已经给我正在使用的CLI读/写权限。这可能是什么原因呢?

在引号内,''转义下一个字符;请参阅有关字符串字面值的参考资料。像

那样双斜杠
os.path.exists('C:\Users\ALPHA')

要转义反斜杠本身,请按照Michael的建议使用正斜杠作为路径分隔符,或者使用"原始字符串":

os.path.exists(r'C:UsersALPHA')

开头的r将导致Python不将反斜杠视为转义字符。这是我最喜欢的处理Windows路径名的解决方案,因为它们看起来仍然像人们期望的那样。

使用双反斜杠或正斜杠:

os.path.exists('C:/Users/ALPHA')    

最新更新