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

问题描述 投票:0回答:5
Python(2.*)中是否有任何“可移植”的方法来获取包含给定路径的设备的文件系统类型?例如,类似:

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

这是我的解决方案。我尝试使其对于 /var/lib 是不同分区的情况更加通用。代码中存在一些丑陋之处,因为 Windows 总是在挂载点末尾有分隔符,而在 Linux 中则省略了分隔符。这意味着同时测试它们
python filesystems
5个回答
4
投票
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')

感谢 user3012759 的评论,这里有一个解决方案(当然可以改进,但仍然有效):

3
投票
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 下需要单独处理“/”,因为所有(绝对)路径都以此开头。

这是“实际运行”的代码示例(GNU/Linux):

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

还有 Windows 下的另一个(如果有的话 XP):

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

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

1
投票
对于linux,安装

0
投票
命令,我用过

P = subprocess.run(['stat', '-f', '-c', '%T', path],
                    capture_output=True)
fstype = P.stdout.strip().lower()

对于任何优先考虑简洁性的人,这里有一个稍微简短的 gena2x 答案版本:

-1
投票
def get_fs_type(path): path_matching_fstypes_mountpoints = [ part for part in disk_partitions() if str(path.resolve()).startswith(part.mountpoint) ] return sorted(path_matching_fstypes_mountpoints, key=lambda x: len(x.mountpoint))[-1].fstype


© www.soinside.com 2019 - 2024. All rights reserved.