python - os.path.exists 在 Python CLI 上无法正常工作

标签 python python-module

我的 Windows 7 计算机上安装了 Python 2.5.x。

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

我已向我正在使用的 CLI 授予读/写权限。 可能的原因是什么?

最佳答案

在引号内,'\' 转义下一个字符;请参阅reference on string literals 。要么加倍反斜杠,例如:

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

要转义反斜杠本身,请按照迈克尔的建议使用正斜杠作为路径分隔符,或使用“原始字符串”:

os.path.exists(r'C:\Users\ALPHA')

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

关于python - os.path.exists 在 Python CLI 上无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681243/

相关文章:

python - 如何在Python中将行字段更改为列标题?

python - 重新采样 Pandas 数据框正在删除列

python - TensorFlow/TFLearn : ValueError: Cannot feed value of shape (64, ) 用于张量 u'target/Y : 0', which has shape ' (? , 10)'

python - ufunc 'add' 不包含签名匹配类型的循环 dtype ('<U23' ) dtype ('<U23' ) dtype ('<U23' )

python - 在Python中使用虚拟环境导入模块

Python 将多个文件模块中的所有内容导入命名空间

python - NLTK - nltk.tokenize.RegexpTokenizer - 正则表达式未按预期工作

Python:替换模块类中的函数

python - 如何在 Python 中加载子目录下的所有模块?

python - Pyautogui 在 IDLE 中工作,但当我将其作为 .py 执行时则不起作用