How to determine if a file has been deleted in Python

I want to determine if a file is on the local hard drive or a drive that is installed from the network in OSX. Therefore, I would like to create code similar to the following:

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

I could not find anything that works like is_local_file() in the above example. Ideally, I would like to use an existing function, if any, but could not, how could I implement it myself? The best I came up with is the following, but this applies to the installed dmgs, as if they are remote, which is not what I want. I also suspect that I can reinvent 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 that generate checksums, one of which uses a multiprocessor that imposes overhead for a start, but which is faster for large files if the network connection is slow.

+6
source share
2 answers

"I have two functions that generate checksums, one of which uses a multiprocessor, which requires overhead to get started, but which is faster for large files if the network connection is slow."

Then what you are really looking for is_local_file() to tell you is just a proxy measure for "will file access be slower than I would like?". As a proxy measure, this is a relatively bad indicator of what you really want to know for all the related reasons mentioned above (local, but virtualized disks, remote but critically fast NAS, etc.).

Since you are asking a question that is almost impossible to answer programmatically, itโ€™s better to specify a parameter, like the -jobs parameter to make , which explicitly says โ€œparallelize this runโ€.

+2
source

You can use your existing code (or try the solution in how to find the file mount point on? ) To find the file mount point; then read /proc/mounts to find the device and file system; /proc/mounts has the format

 device mountpoint filesystem options... 

You can use the filesystem field to automatically exclude known network file systems, for example. afs, cifs, nfs, smbfs. Otherwise, you can look at the device; as a basic heuristic, if the device is a node device ( stat.S_ISBLK ) or none , then the file system is probably local; if it is in URI style ( host:/path ), then it is probably deleted; if it is an actual file, then the file system is a disk image and you will need to overwrite.

+1
source

Source: https://habr.com/ru/post/921293/


All Articles