os.path.isfile() 为网络驱动器上的文件返回 false [英] os.path.isfile() returns false for file on network drive

查看:75
本文介绍了os.path.isfile() 为网络驱动器上的文件返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 os.path.isfile 测试网络驱动器上是否存在文件,但是即使文件存在,它也会返回 false.任何想法可能是为什么或我可以用来检查此文件的任何其他方法?

I am trying to test if a file exists on the network drive using os.path.isfile however it returns false even when the file is there. Any ideas why this might be or any other methods I could use to check for this file?

我使用的是 Python2.7 和 Windows10

I am using Python2.7 and Windows10

它应该返回真:

import os

if os.path.isfile("C:\test.txt"):
    print "Is File"
else:
    print "Is Not File"

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

This returns false even though the file exists:

import os

if os.path.isfile("Q:\test.txt"):
    print "Is File"
else:
    print "Is Not File"

推荐答案

来自 python https://docs.python.org/2/library/os.path.html:

From python https://docs.python.org/2/library/os.path.html:

os.path 模块始终是适用于运行 Python 的操作系统的路径模块,因此可用于本地路径

The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths

尝试使用完整的 UNC 路径而不是映射的驱动器.

Trying using the full UNC path instead of the mapped drive.

import os

if os.path.isfile(r"\\full\uncpath\test.txt"):
    print "Is File"
else:
    print "Is Not File"

这篇关于os.path.isfile() 为网络驱动器上的文件返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆