how to determine if files are on same or different file systems
Skip Montanaro
skip at pobox.com
Sat Dec 6 18:39:03 EST 2003
Kamus> It would be nice if I could write a function that would determine
Kamus> if the source and destination are on the same file system or not,
Kamus> and thus use rename or copy appropriately, or if there is already
Kamus> such a built-in function.
You might want to check out os.path.ismount(). Using it, you can walk up
the two paths toward the root until you hit elements of bot paths where
os.path.ismount() returns True. If the two mount points are the same, the
two original paths are on the same file system. Here's some sample code.
It doesn't help much on my Mac because it essentially has just a single disk
partition. On my web server (Linux w/ multiple partitions), it seems to
work okay:
% df
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda1 248847 93239 142758 40% /
/dev/hda6 14333167 9393563 4194190 70% /backup
none 385572 0 385572 0% /dev/shm
/dev/hda5 9867356 7702853 1652433 83% /home
/dev/hda7 1981000 1568913 309675 84% /usr
/dev/hda8 248847 120886 115111 52% /var
% python samefs.py
('/', '/usr') 0
('/bin', '/etc') 1
('/etc/passwd', '/var/log') 0
('/Applications', '/Volumes Kisses In The Rain') 0
Skip
import os
def samefs(path1, path2):
if not (os.path.exists(path1) and os.path.exists(path2)):
return False
while path1 != os.path.dirname(path1):
if os.path.ismount(path1):
break
path1 = os.path.dirname(path1)
while path2 != os.path.dirname(path2):
if os.path.ismount(path2):
break
path2 = os.path.dirname(path2)
return path1 == path2
print ("/", "/usr"), samefs("/", "/usr")
print ("/bin", "/etc"), samefs("/bin", "/etc")
print ("/etc/passwd", "/var/log"), samefs("/etc/passwd", "/var/log")
print ("/Applications", "/Volumes Kisses In The Rain"), \
samefs("/Applications", "/Volumes Kisses In The Rain")
More information about the Python-list
mailing list