繁体   English   中英

为什么python os.path.isfile在Windows上仅针对特定目录返回false?

[英]Why does python os.path.isfile return false on windows, for only a specific directory?

在Windows 7上,当我运行此Python 2.7代码时,将打印“ NOT file”,但文件在那里,它不是只读的,文件夹不是只读的,其父级也不是只读的。

if os.path.exists('D:\testfiles\mysub\GraphiteController.js'):
    print "IS file"
else:
    print "NOT file"
sys.exit(1)

如果我将文件移动到d:\\myother directory ,则输出“ IS文件”。 如果我将文件移动到d:\\testfiles directory ,则显示“ NOT file”。

我在另一台Windows机器上尝试了这个,同样的问题。 很奇怪。

这是因为'\\ t'是制表符。 在路径中使用正斜杠或使用原始字符串:

if os.path.exists('D:/testfiles/mysub/GraphiteController.js'):

要么

if os.path.exists(r'D:\testfiles\mysub\GraphiteController.js'):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM