I’m trying to create a simple script that will select specific columns from the unix df - h command. I can use awk to do this but how can we do this in python?
Here is df -h output:
Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_base-lv_root 28G 4.8G 22G 19% / tmpfs 814M 176K 814M 1% /dev/shm /dev/sda1 485M 120M 340M 27% /boot
I want something like:
Column 1:
Filesystem /dev/mapper/vg_base-lv_root tmpfs /dev/sda1
Column 2:
Size 28G 814M 485M
You can use
op.popento run the command and retrieve its output, thensplitlinesandsplitto split the lines and fields. Rundf -Phrather thandf -hso that lines are not split if a column is too long.The result is a list of lines. To extract the first column, you can use
[line[0] for line in df_output_lines](note that columns are numbered from 0) and so on. You may want to usedf_output_lines[1:]instead ofdf_output_linesto strip the title line.If you already have the output of
df -hstored in a file somewhere, you’ll need to join the lines first.Note that this assumes that neither the filesystem name nor the mount point contain whitespace. If they do (which is possible with some setups on some unix variants), it’s practically impossible to parse the output of
df, evendf -P. You can useos.statvfsto obtain information on a given filesystem (this is the Python interface to the C function thatdfcalls internally for each filesystem), but there’s no portable way of enumerating the filesystems.