在 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 , 打印“非文件”。

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

请您参考如下方法:

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

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

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


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!