473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

calculate system disk space

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

Dec 9 '05 #1
6 3734

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


Google, on "Python disk size", returned this link:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66455

Thanks for the reply recipe, Steve ;-)

Jean-Marc

Dec 9 '05 #2
I am looking for unix.the recipe is windows specific!!

Dec 9 '05 #3
PyPK wrote:
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.
Dec 10 '05 #4
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_p ct,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(No ne,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_p ct,mount=df_list.split()
#mount_list.append(mount)
return(0,int(disc_size),int(disc_used))


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

Dec 10 '05 #5
ra************@gmail.com wrote:
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.
Dec 10 '05 #6
Heiko Wundram:
Under Windows you don't have sparse files though, so there
are no fields ...


Does too!
http://msdn.microsoft.com/library/en...set_sparse.asp
They're fairly rare though.

Neil
Dec 10 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
9
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to...
5
by: LP | last post by:
Hello, We running VB.NET application which gets massive amounts of data from SQL Server, loads data into DataTables, then re-arranges data into tabular structure and outputs it to a flat file....
2
by: smeagol | last post by:
Hi, I keep getting this error message for a trans.log backup. Operating system error 112(error not found). The disk has about 6GB space free, and the backup should only take up about 550 MB, so...
1
by: mattias192 | last post by:
I cannot make sense of the ODBC error messages my VBA application throws at me. I connect to an Access database of about 500MB in size. First, there is the "Not enough space on temporary disk"....
3
by: Anil Gupte | last post by:
I am getting this error: Type 'System.Management.ManagementClass' is not defined. The statement is: Dim diskClass As New System.Management.ManagementClass("Win32_LogicalDisk") According to the...
3
by: Tonij (with a J) | last post by:
Hi all, I have an Access 2003 database that has been a work in progress for a while, it's basically a system inventory and issue tracking system. Recently I have added a seperate area for...
0
by: trazcure | last post by:
Hi everyone, my name is Traz I'm trying to write some C# code that will check for remote disk space on network shares. I have seen across the internet and MSDN that you can utilize the...
4
by: raidvvan | last post by:
Hi there, We have been looking for some time now for a database system that can fit a large distributed computing project, but we haven't been able to find one. I was hoping that someone can...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.