4

How do I determine if a given path or drive is formatted to EXT4, EXT3, EXT2, FAT32, NTFS or some such, in Python?

martineau
  • 112,593
  • 23
  • 157
  • 280
Brōtsyorfuzthrāx
  • 3,959
  • 3
  • 33
  • 53
  • Related: [Find size and free space of the filesystem containing a given file](http://stackoverflow.com/q/4260116/279627) – Sven Marnach Apr 15 '14 at 10:55

1 Answers1

6

psutil is a cross-platform package which can 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')]

Warning: On linux, the fstype may be reported as ext4 or ntfs, but on Windows, the fstype is limited to "removable", "fixed", "remote", "cdrom", "unmounted" or "ramdisk".

unutbu
  • 777,569
  • 165
  • 1,697
  • 1,613
  • Awesome. Thanks. If there were a way to find out the file system of a specific directory, that would be a great addition to your answer. This helps anyhow, though. – Brōtsyorfuzthrāx Apr 16 '14 at 01:37
  • 2
    On Linux, you could try parsing the output of `df -TP path`, but doing so could be very tricky since the device name and mount point can contain spaces. – unutbu Apr 16 '14 at 07:43
  • Looks like on you can now get the actual fstype on Windows, so your previous warning is now 'invalid'. I can confirm it works, but also see this commit message for psutil: https://github.com/giampaolo/psutil/issues/209#issuecomment-44019539 – Patrick Oct 30 '16 at 08:04