os.path.isfile()对网络驱动器上的文件返回false
问题描述:
我试图使用os.path.isfile来测试网络驱动器上是否存在文件,但即使文件存在,它也会返回false。任何想法,为什么这可能是我可以用来检查这个文件的任何其他方法?os.path.isfile()对网络驱动器上的文件返回false
我使用Python2.7和Windows10
这个,因为它应该返回true:
import os
if os.path.isfile("C:\test.txt"):
print "Is File"
else:
print "Is Not File"
这个返回false,即使该文件存在:
import os
if os.path.isfile("Q:\test.txt"):
print "Is File"
else:
print "Is Not File"
答
蟒蛇https://docs.python.org/2/library/os.path.html:
的os.path中模块总是适合于操作系统的Python在其上运行,并且因此可用于本地路径
使用完整的UNC路径,而不是映射的驱动器尝试的路径的模块。
import os
if os.path.isfile(r"\\full\uncpath\test.txt"):
print "Is File"
else:
print "Is Not File"
+0
谢谢你这个作品! –
+0
@TomCarroll甜蜜!我很高兴能帮助你 –
使用原始字符串:'os.path.isfile(r“Q:\ test.txt”)'? –
也返回false –
'os.path.exists()'返回什么? –