calculate system disk space

P

PyPK

how can we compute the current system disk space using a python
script.?
any ideas or have anyone tried this..
 
H

Heiko Wundram

PyPK said:
I am looking for unix.the recipe is windows specific!!

Parse the output of du/df? :) I guess that would be simplest... ;-)

Otherwise, use some combination of os.walk() and os.stat(), whereby you
_don't_ use the stat.st_size field to get the file size (on disk) but
rather use stat.st_blocks*stat.st_blksize to get it, as you might come
across so called sparse files which are bigger than their on-disk
representation.

For explanations, see:

http://www.python.org/doc/2.4.2/lib/os-file-dir.html
(entries for stat() and walk())

--- Heiko.
 
R

rainbow.cougar

A little somehting I rigged up when I found the Python call to be Linux
specific:

"""
mount_list
Taste the system and return a list of mount points.
On UNIX this will return what a df will return
On DOS based systems run through a list of common drive letters and
test them
to see if a mount point exists. Whether a floppy or CDROM on DOS is
currently active may present challenges.
Curtis W. Rendon 6/27/200 v.01
6/27/2004 v.1 using df to make portable, and some DOS tricks to get
active
drives. Will try chkdsk on DOS to try to get drive size as statvfs()
doesn't exist on any system I have access to...

"""
import sys,os,string
from stat import *

def mount_list():
"""
returns a list of mount points
"""


doslist=['a:\\','b:\\','c:\\','d:\\','e:\\','f:\\','g:\\','h:\\','i:\\','j:\\','k:\\','l:\\','m:\\','n:\\','o:\\','p:\\','q:\\','r:\\','s:\\','t:\\','u:\\','v:\\','w:\\','x:\\','y:\\','z:\\']
mount_list=[]

"""
see what kind of system
if UNIX like
use os.path.ismount(path) from /... use df?
if DOS like
os.path.exists(path) for a list of common drive letters
"""
if sys.platform[:3] == 'win':
#dos like
doslistlen=len(doslist)
for apath in doslist:
if os.path.exists(apath):
#maybe stat check first... yeah, it's there...
if os.path.isdir(apath):
mode = os.stat(apath)
try:
dummy=os.listdir(apath)
mount_list.append(apath)
except:
continue
else:
continue
return (mount_list)

else:
#UNIX like
"""
AIX and SYSV are somewhat different than the GNU/BSD df, try to
catch
them. This is for AIX, at this time I don't have a SYS5 available
to see
what the sys.platform returns... CWR
"""
if 'aix' in sys.platform.lower():
df_file=os.popen('df')
while True:
df_list=df_file.readline()
if not df_list:
break #EOF
dflistlower = df_list.lower()
if 'filesystem' in dflistlower:
continue
if 'proc' in dflistlower:
continue

file_sys,disc_size,disc_avail,disc_cap_pct,inodes,inodes_pct,mount=df_list.split()
mount_list.append(mount)

else:
df_file=os.popen('df')
while True:
df_list=df_file.readline()
if not df_list:
break #EOF
dflistlower = df_list.lower()
if 'filesystem' in dflistlower:
continue
if 'proc' in dflistlower:
continue

file_sys,disc_size,disc_used,disc_avail,disc_cap_pct,mount=df_list.split()
mount_list.append(mount)

return (mount_list)



"""
have another function that returns max,used for each...
maybe in discmonitor
"""
def size(mount_point):
"""
"""
if sys.platform[:3] == 'win':
#dos like
dos_cmd='dir /s '+ mount_point
check_file=os.popen(dos_cmd)
while True:
check_list=check_file.readline()
if not check_list:
break #EOF
if 'total files listed' in check_list.lower():
check_list=check_file.readline()

if 'file' in check_list.lower():
if 'bytes' in check_list.lower():
numfile,filtxt,rawnum,junk=check_list.split(None,3)
total_used=string.replace(rawnum,',','')
#return (0,int(total_size),int(total_size))
#break
check_list=check_file.readline()
if 'dir' in check_list.lower():
if 'free' in check_list.lower():
numdir,dirtxt,rawnum,base,junk=check_list.split(None,4)
multiplier=1
if 'mb' in base.lower():
multiplier=1000000
if 'kb' in base.lower():
multiplier=1000

rawnum=string.replace(rawnum,',','')
free_space=float(rawnum)*multiplier
#print
(0,int(free_space)+int(total_used),int(total_used))
return
(0,int(free_space)+int(total_used),int(total_used))
else:
continue



else:
#UNIX like
"""
AIX and SYSV are somewhat different than the GNU/BSD df, try to
catch
them. This is for AIX, at this time I don't have a SYS5 available
to see
what the sys.platform returns... CWR
"""
df_cmd = 'df '+ mount_point
if 'aix' in sys.platform.lower():
df_file=os.popen(df_cmd)
while True:
df_list=df_file.readline()
if not df_list:
break #EOF
dflistlower = df_list.lower()
if 'filesystem' in dflistlower:
continue
if 'proc' in dflistlower:
continue

file_sys,disc_size,disc_avail,disc_cap_pct,inodes,inodes_pct,mount=df_list.split()
return(0,int(disc_size),int(disc_size)-int(disc_avail))

else:
df_file=os.popen(df_cmd)
while True:
df_list=df_file.readline()
if not df_list:
break #EOF
dflistlower = df_list.lower()
if 'filesystem' in dflistlower:
continue
if 'proc' in dflistlower:
continue

file_sys,disc_size,disc_used,disc_avail,disc_cap_pct,mount=df_list.split()
#mount_list.append(mount)
return(0,int(disc_size),int(disc_used))




if __name__ == '__main__':
print(mount_list())
 
H

Heiko Wundram

A little somehting I rigged up when I found the Python call to be Linux
specific:

os.stat isn't Linux-specific, isn't even Unix-specific, works just fine
under Windows. Under Windows you don't have sparse files though, so there
are no fields which give you the block-size of the device or the
block-count of a file, just the st_size field which gives you the data size
of the file.

--- Heiko.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top