【问题标题】:How to get the filesystem of a directory or drive?如何获取目录或驱动器的文件系统?
【发布时间】:2014-04-15 10:46:58
【问题描述】:

如何在 Python 中确定给定路径或驱动器是否被格式化为 EXT4、EXT3、EXT2、FAT32、NTFS 或类似格式?

【问题讨论】:

标签: python filesystems


【解决方案1】:

psutil是一个跨平台的包,可以identify partition types:

>>> 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 可能报告为 ext4ntfs,但在 Windows 上,fstype 被限制为 "removable", "fixed", "remote", "cdrom", "unmounted" or "ramdisk"

【讨论】:

  • 太棒了。谢谢。如果有办法找出特定目录的文件系统,那将是您答案的一个很好的补充。不过,这无论如何都会有所帮助。
  • 在 Linux 上,您可以尝试解析 df -TP path 的输出,但这样做可能非常棘手,因为设备名称和挂载点可以包含空格。
  • 看起来你现在可以在 Windows 上获得实际的 fstype,所以你之前的警告现在是“无效的”。我可以确认它有效,但也可以看到 psutil 的此提交消息:github.com/giampaolo/psutil/issues/209#issuecomment-44019539
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 2014-10-14
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多