【问题标题】:os.path.isfile() returns false for file on network driveos.path.isfile() 为网络驱动器上的文件返回 false
【发布时间】:2017-01-22 09:11:20
【问题描述】:

我正在尝试使用 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"

【问题讨论】:

  • 使用原始字符串:os.path.isfile(r"Q:\test.txt")?
  • 也返回 false
  • os.path.exists() 返回什么?
  • os.path.exists() 也返回 false

标签: python python-2.7


【解决方案1】:

来自pythonhttps://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"

【讨论】:

  • @TomCarroll 甜蜜!很高兴能帮到你
猜你喜欢
  • 2019-11-14
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 2012-12-31
  • 1970-01-01
相关资源
最近更新 更多