df
is primarily intended for human consumption and only sometimes for scripting in shell script. The output of user space commands can sometimes be hard to parse as they're primarily intended for human consumption, though you can pass some arguments to some user space commands to have machine-parseable output. When using languages like Python, os
supports most of the commonly used system features, but you can also use high level wrappers like psutil
library. I highly recommend psutil
if you're doing this often.
If you don't want to bring in third party libraries, I'd recommend using the /sys/class/block
special filesystem (or /sys/block
if you want to support legacy systems as well), alternatively you can parse /proc/partitions
. The /sys
, /dev
, and /proc
special filesystems are stable kernel interfaces designed for use in scripting, you interact with these special files by reading/writing to these special files, most of the interfaces the there are fairly easy to parse as they're designed to be used in shell scripts.