从Python中的路径确定文件系统类型
Python(2.*)中是否有任何可移植的方法来获取包含给定路径的设备的文件系统类型?例如,类似于:从Python中的路径确定文件系统类型,python,filesystems,Python,Filesystems,Python(2.*)中是否有任何可移植的方法来获取包含给定路径的设备的文件系统类型?例如,类似于: >>> get_fs_type("/foo/bar") 'vfat' 感谢user3012759的评论,这里有一个解决方案(当然可以改进,但仍然有效): GNU/Linux下的“/”需要单独处理,因为所有(绝对)路径都以“/”开头 下面是代码“正在运行”(GNU/Linux)的一个示例: 还有一个在Windows下(XP,如果有关系的话): 这是我的解决办法。我试图使它更通用
>>> get_fs_type("/foo/bar")
'vfat'
感谢user3012759的评论,这里有一个解决方案(当然可以改进,但仍然有效): GNU/Linux下的“/”需要单独处理,因为所有(绝对)路径都以“/”开头 下面是代码“正在运行”(GNU/Linux)的一个示例: 还有一个在Windows下(XP,如果有关系的话):
这是我的解决办法。我试图使它更通用于/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')
导入psutil
def get_fs_类型(路径):
最佳匹配=“”
fsType=“”
对于psutil.disk_分区()中的部分:
如果mypath.startswith(part.mountpoint)和len(bestMatch)
可能会有帮助。不确定,但可能会这样做。这不起作用,因为您可能有如下嵌套装载:/-root mount/usr——其他类型/usr/tmp-其他类型;此答案中提供的代码将为/usr/tmp/myfile中的文件返回/usr类型。psutil
是一个巨大的导入!有没有办法限制psutil导入的import XXX from psutil
?对于这种特定情况,唯一有用的psutil
功能是disk\u partitions
,所以是:from psutil import disk\u partitions
。确实是最好的答案,通过使用平台模块,您可以避免检查不正确的装入点,如:不太重要可以节省执行时间:这与其他答案有什么不同?代码复杂度不同,并且在所有情况下都可以工作-请参阅其他答案的说明。
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))
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:\\')
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')
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