os.path.isfile() 对于网络驱动器上的文件返回 false

Tom*_*oll 4 python python-2.7

我正在尝试使用 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"
Run Code Online (Sandbox Code Playgroud)

即使文件存在,也会返回 false:

import os

if os.path.isfile("Q:\test.txt"):
    print "Is File"
else:
    print "Is Not File"
Run Code Online (Sandbox Code Playgroud)

hax*_*ode 6

来自 python 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"
Run Code Online (Sandbox Code Playgroud)