PyPK wrote:[color=blue]
> I am looking for unix.the recipe is windows specific!![/color]
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.
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
"""
"""
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(a path):
mode = os.stat(apath)
try:
dummy=os.listdi r(apath)
mount_list.appe nd(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.lo wer():
df_file=os.pope n('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
else:
df_file=os.pope n('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
"""
have another function that returns max,used for each...
maybe in discmonitor
"""
def size(mount_poin t):
"""
"""
if sys.platform[:3] == 'win':
#dos like
dos_cmd='dir /s '+ mount_point
check_file=os.p open(dos_cmd)
while True:
check_list=chec k_file.readline ()
if not check_list:
break #EOF
if 'total files listed' in check_list.lowe r():
check_list=chec k_file.readline ()
if 'file' in check_list.lowe r():
if 'bytes' in check_list.lowe r():
numfile,filtxt, rawnum,junk=che ck_list.split(N one,3)
total_used=stri ng.replace(rawn um,',','')
#return (0,int(total_si ze),int(total_s ize))
#break
check_list=chec k_file.readline ()
if 'dir' in check_list.lowe r():
if 'free' in check_list.lowe r():
numdir,dirtxt,r awnum,base,junk =check_list.spl it(None,4)
multiplier=1
if 'mb' in base.lower():
multiplier=1000 000
if 'kb' in base.lower():
multiplier=1000
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.lo wer():
df_file=os.pope n(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
else:
df_file=os.pope n(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
rainbow.cougar@ gmail.com wrote:[color=blue]
> A little somehting I rigged up when I found the Python call to be Linux
> specific:[/color]
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.
Comment