从Python中的路径确定文件系统类型

时间:2014-08-13 10:31:58

标签: python filesystems

Python(2. *)中是否有 portable 方法来获取包含给定路径的设备的文件系统类型?例如,像:

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

3 个答案:

答案 0 :(得分:3)

这是我的解决方案。在/ var / lib是不同的分区的情况下,我试图使它更通用。代码中有些丑陋,因为windows总是在挂载点的末尾有分隔符,而在linux中省略了这一点。这意味着对它们进行测试

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))

在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:\\')

在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 :(得分:1)

感谢user3012759的评论,这是一个解决方案(当然可以改进,但仍在工作):

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

在GNU / Linux下,“/”需要单独处理,因为所有(绝对)路径都以此开头。

以下是代码“in action”(GNU / Linux)的示例:

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

Windows下的另一个(XP,如果重要的话):

>>> get_fs_type("C:\\")  # careful: "C:" will yield ''
'NTFS'

答案 2 :(得分:0)

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