0

I have the following python script which is called through a java class. It works fine with local filenames ('D:\temp\Test.pdf') but when the filename is \serverA\f$\dir\Test.pdf, it always returns false. It is running on a tomcat server (with Admin rights) and the serverA drive f is mounted on the tomcat server machine. Any ideas on what I might be missing?

def checkFileExists(filename):

        vFile = File(filename)
        if (vFile == None):
            return False
        return vFile.exists()
Wooble
  • 87,717
  • 12
  • 108
  • 131
  • If the file is on a network drive, the problem is a bit deeper than it might seem. See http://stackoverflow.com/questions/1271317/what-is-the-best-way-to-map-windows-drives-using-python. Also in future note the standard `os.path.exists` function. – Zaur Nasibov Feb 28 '13 at 13:19
  • Thanks for that but I don't have the luxury to map and unmap drives myself on that system. The drive that the files are located is mapped on the tomcat server unfortunately. So, you say there is no standard solution for that? – user2119684 Feb 28 '13 at 13:27
  • By the way is the path is `\serverA\...`? or is it `\\serverA\...`? – Zaur Nasibov Feb 28 '13 at 13:36
  • It's \\ServerA\f$ so it is a mapped drive (f:) on a remote machine (serverA). – user2119684 Feb 28 '13 at 13:42
  • Can you please run one more test with command line: `IF EXIST \\serverA\$f\dir\Test.pdf ECHO '1'`? The idea is to launch an internal windows tool from Python and parsing its output :) – Zaur Nasibov Feb 28 '13 at 13:53
  • It returns '1' so it's there. I don't really get your idea, could you be more specific? Many thanks – user2119684 Feb 28 '13 at 14:02
  • See the updated answer. I hope that it works :) – Zaur Nasibov Feb 28 '13 at 14:15

1 Answers1

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))
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
  • Thanks for that, that actually works for tricking python and checking if the file is there indeed but still, I don't get the file itself.. The point of the whole operation is to move some files from the remote location to a local drive. So the real issue here is that after vFile=File(pathname), vFile equals None and vFile.exists() returns false.. Do you have any idea why may that happen? Thanks again – user2119684 Feb 28 '13 at 14:31
  • Just use the `xcopy` command in the similar way, e.g. `subprocess.call(['xcopy', source_path, dest_path])` – Zaur Nasibov Feb 28 '13 at 14:55