Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a file on the same filesystem as another file in python?

Is there a simple way of finding out if a file is on the same filesystem as another file?

The following command:

import shutil
shutil.move('filepatha', 'filepathb')

will try and rename the file (if it's on the same filesystem), otherwise it will copy it, then unlink.

I want to find out before calling this command whether it will preform the quick or slow option, how do I do this?

like image 695
Dan Avatar asked Dec 23 '22 11:12

Dan


1 Answers

Use os.stat (on a filename) or os.fstat (on a file descriptor). The st_dev of the result will be the device number. If they are on the same file system, it will be the same in both.

import os

def same_fs(file1, file2):
    dev1 = os.stat(file1).st_dev
    dev2 = os.stat(file2).st_dev
    return dev1 == dev2
like image 140
Jay Conrod Avatar answered Mar 09 '23 01:03

Jay Conrod