0

我有以下通过 java 类调用的 python 脚本。它适用于本地文件名('D:\temp\Test.pdf'),但当文件名是 \serverA\f$\dir\Test.pdf 时,它总是返回 false。它在 tomcat 服务器上运行(具有管理员权限),并且 serverA 驱动器 f 安装在 tomcat 服务器机器上。关于我可能遗漏的任何想法?

def checkFileExists(filename):

        vFile = File(filename)
        if (vFile == None):
            return False
        return vFile.exists()
4

1 回答 1

0

So, as discussed in comments to the question, it is a bit hard to access Windows shares in Python. So, a hacky way of checking whether a file exists in a remote location not understood by Python, but understood by Windows' tools would be invoking these tools and parsing their output.

For example:

import subprocess

def file_exists(path):        
    res = subprocess.check_output(['IF', 'EXIST', path, 'ECHO', "1"])
    return res.strip() == '1'

Usage:

path = r'\\serverA\f$\dir\Test.pdf'
print(file_exists(path))
于 2013-02-28T13:17:29.037 回答