[Tutor] df type function in python
Roel Schroeven
rschroev_nospam_ml at fastmail.fm
Wed Jan 7 20:51:02 CET 2009
Spencer Parker schreef:
> I am looking for a function in python that would operate similar to df.
> I would use df, but I am unable to parse the data that it gives me from
> there. If it would give me a single line I could then split it out if I
> needed to, but I can't see how to actually do that. What I am trying to
> do is get some information on a drive I have mounted. I have a disk
> image that I have mounted on temp folder that I need to see how much
> total pace it is taking up. It gives me a more accurate representation
> of my xen disk images that just an ls -sk or something on the file.
Some time ago I wrote something like that for a script of mine:
import subprocess
def df(filesystem):
"""Report disk usage.
Return a dictionary with total, used, available. Sizes are reported
in blocks of 1024 bytes."""
output = subprocess.Popen(
['/bin/df', '-B 1024', filesystem],
stdout=subprocess.PIPE).communicate()[0]
lines = output.split('\n')
fs, blocks_total, blocks_used, blocks_available, used, mount =
lines[1].split()
blocks_total = int(blocks_total)
blocks_used = int(blocks_used)
blocks_available = int(blocks_available)
return dict(
total=blocks_total,
used=blocks_used,
available=blocks_available)
I use from a cronjob in a script that monitors the disk usage and sends
me a mail if the disk gets too full.
--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov
Roel Schroeven
More information about the Tutor
mailing list