在Python中,如何确定给定的路径或驱动器是否格式化为EXT4、EXT3、EXT2、FAT32、NTFS或类似格式?
在Python中,如何确定给定的路径或驱动器是否格式化为EXT4、EXT3、EXT2、FAT32、NTFS或类似格式?
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'),
sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext4', opts='rw')]
警告:在Linux上,fstype可能报告为ext4
或ntfs
,但是在Windows上,fstype限制为"removable"、"fixed"、"remote"、"cdrom"、"unmounted"或"ramdisk"
。
df -TP path
的输出,但这样做可能会非常棘手,因为设备名称和挂载点可能包含空格。 - unutbudf -T
命令来打印文件系统(在我的机器上,df --print-type
也不起作用)。请参阅 man page 获取更多信息(它建议使用 lsvfs
命令显示可用的文件系统)。import psutil
import os
def extract_fstype(path_=os.getcwd()):
"""Extracts the file system type of a given path by finding the mountpoint of the path."""
for i in psutil.disk_partitions(all=True):
if path_.startswith(i.mountpoint):
if i.mountpoint == '/': # root directory will always be found
# print(i.mountpoint, i.fstype, 'last resort') # verbose
last_resort = i.fstype
continue
# print(i.mountpoint, i.fstype, 'return') # verbose
return i.fstype
return last_resort