Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if file is remote in Python

Tags:

python

I want to determine whether or not a file is located on a local hard drive or a drive mounted from the network in OSX. So I'd be looking to produce code a bit like the following:

file_name = '/Somewhere/foo.bar'
if is_local_file(file_name):
    do_local_thing()
else:
    do_remote_thing()

I've not been able to find anything that works like is_local_file() in the example above. Ideally I'd like to use an existing function if there is one but failing that how could I implement it myself? The best I've come up with is the following but this treats mounted dmgs as though they're remote which isn't what I want. Also I suspect I might be reinventing the wheel!

def is_local_file(path):
    path = path.split('/')[1:]
    for index in range(1,len(path)+1):
        if os.path.ismount('/' + '/'.join(path[:index])):
            return False
    return True

I have two functions which generate checksums, one of which uses multiprocess which incurs an overhead to start off with but which is faster for large files if the network connection is slow.

like image 584
redrah Avatar asked Jul 25 '12 11:07

redrah


1 Answers

"I have two functions which generate checksums, one of which uses multiprocess which incurs an overhead to start off with but which is faster for large files if the network connection is slow."

Then what you're really looking for is_local_file() to tell you is only a proxy measure for "will file access be slower than I'd like?". As a proxy measure, it is a relatively poor indicator of what you really want to know for all the confounding reasons noted above (local but virtualized disks, remote but screamingly fast NAS, etc.)

Since you are asking a question that is nearly impossible to answer programatically, it is better to provide an option, as with the -jobs option on make which explicitly says "parallelize this run".

like image 165
msw Avatar answered Nov 07 '22 11:11

msw