Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python os.path.isfile在windows上仅针对特定目录返回false?_Python_Python 2.7 - Fatal编程技术网

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

为什么python os.path.isfile在windows上仅针对特定目录返回false?,python,python-2.7,Python,Python 2.7,在Windows7上,当我运行这个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目录,则打印“IS file”。 如果我将文件移动到d

在Windows7上,当我运行这个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目录
,则打印“IS file”。 如果我将文件移动到
d:\testfiles目录
,则打印“notfile”


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

因为“\t”是制表符。在路径中使用前斜杠或使用原始字符串:

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


抢手货在处理文件路径时,我经常忘记使用原始字符串,并遇到同样的问题;这里没有反斜杠来结束字符串…:)没有指向此部分。指向“应该避开反斜杠,但不会变得漂亮”,然后指向“您也可以使用正斜杠,Python将为您转换”,Python实际上不会转换它们。Windows在系统调用级别接受它们。
if os.path.exists(r'D:\testfiles\mysub\GraphiteController.js'):