How do I determine if a given path or drive is formatted to EXT4, EXT3, EXT2, FAT32, NTFS or some such, in Python?
-
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
2 Answers
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"
.

- 842,883
- 184
- 1,785
- 1,677
-
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
-
2On 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
Although it has been a while since the question and answer were posted, I just want to add a little function that you can use to actually find the file system of a given path. The answer extends unutbu's answer.
The answer is also quite useful for macOS users, where it is not possible to print the file system with df -T
(also at my machine df --print-type
did also not work). See the man page for more information (it suggests using the lsvfs
command to display the available file systems).
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
(tested on macOS and linux)

- 311
- 1
- 9