3

Is there any portable way in Python (2.*) to obtain the filesystem type of the device containing a given path? For instance, something like:

>>> get_fs_type("/foo/bar")
'vfat'
2
3

Thanks to user3012759's comment, here's a solution (certainly improvable upon but nonetheless working):

import psutil

def get_fs_type(mypath):
    root_type = ""
    for part in psutil.disk_partitions():
        if part.mountpoint == '/':
            root_type = part.fstype
            continue

        if mypath.startswith(part.mountpoint):
            return part.fstype

    return root_type

A separate treatment was needed for "/" under GNU/Linux, since all (absolute) paths start with that.

Here's an example of the code "in action" (GNU/Linux):

>>> get_fs_type("/tmp")
'ext4'
>>> get_fs_type("/media/WALKMAN")
'vfat'

And another one under Windows (XP if it matters):

>>> get_fs_type("C:\\")  # careful: "C:" will yield ''
'NTFS'
3
  • This would not work because you may have nested mounts like this: / - root mount; /usr - other type; /usr/tmp - other type; code provided in this answer will return type of /usr for file in /usr/tmp/myfile.
    – gena2x
    Feb 9 '16 at 11:59
  • psutil is a huge import! Is there a way to limit the psutil import with import XXX from psutil?
    – not2qubit
    Nov 21 '18 at 10:46
  • 1
    The only useful psutil function for this specific case is disk_partitions, so yes: from psutil import disk_partitions. Nov 22 '18 at 7:54
3

Here is my solution. I tried to make it more generic for cases where /var/lib is a different partition. Some ugliness crept in the code as windows always has the separator at the end of the mountpoint, while this is omitted in linux. Which means testing them both

import psutil, os
def printparts():
    for part in psutil.disk_partitions():
        print part
def get_fs_type(path):
    partition = {}
    for part in psutil.disk_partitions():
        partition[part.mountpoint] = (part.fstype, part.device)
    if path in partition:
        return partition[path]
    splitpath = path.split(os.sep)  
    for i in xrange(len(splitpath),0,-1):
        path = os.sep.join(splitpath[:i]) + os.sep
        if path in partition:
            return partition[path]
        path = os.sep.join(splitpath[:i])
        if path in partition:
            return partition[path]
    return ("unkown","none")

printparts()

for test in ["/", "/home", "/var", "/var/lib", "C:\\", "C:\\User", "D:\\"]:
    print "%s\t%s" %(test, get_fs_type(test))

On windows:

python test.py
sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed')
sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed')
sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed')
sdiskpart(device='F:\\', mountpoint='F:\\', fstype='', opts='cdrom')
sdiskpart(device='G:\\', mountpoint='G:\\', fstype='', opts='cdrom')
/       ('unkown', 'none')
/home   ('unkown', 'none')
/var    ('unkown', 'none')
/var/lib        ('unkown', 'none')
C:\     ('NTFS', 'C:\\')
C:\User ('NTFS', 'C:\\')
D:\     ('NTFS', 'D:\\')

On linux:

python test.py
partition(device='/dev/cciss/c0d0p1', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro')
partition(device='/dev/cciss/c0d1p3', mountpoint='/home', fstype='ext4', opts='rw')
partition(device='/dev/cciss/c0d1p2', mountpoint='/var', fstype='ext4', opts='rw')
/       ('ext4', '/dev/cciss/c0d0p1')
/home   ('ext4', '/dev/cciss/c0d1p3')
/var    ('ext4', '/dev/cciss/c0d1p2')
/var/lib        ('ext4', '/dev/cciss/c0d1p2')
C:\     ('unkown', 'none')
C:\User ('unkown', 'none')
D:\     ('unkown', 'none')
1
  • Best answer indeed, you could have avoided to check improper mountpoint by using the platform module as in: docs.python.org/2/library/platform.html Not quite important be could save execution time :D
    – Dr I
    Jun 3 '16 at 22:13
1
import psutil

def get_fs_type(path):
    bestMatch = ""
    fsType = ""
    for part in psutil.disk_partitions():
        if mypath.startswith(part.mountpoint) and len(bestMatch) < len(part.mountpoint):
            fsType = part.fstype
            bestMatch = part.mountpoint
    return fsType
2
  • How is this different from the other answers?
    – not2qubit
    Nov 21 '18 at 10:44
  • Code complexity different and it would actually work in all cases - see descrition on the other answer.
    – gena2x
    Nov 26 '18 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.