calculate system disk space

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PyPK

    #1

    calculate system disk space

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

  • jmdeschamps@gmail.com

    #2
    Re: calculate system disk space


    PyPK wrote:[color=blue]
    > how can we compute the current system disk space using a python
    > script.?
    > any ideas or have anyone tried this..[/color]

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



    Thanks for the reply recipe, Steve ;-)

    Jean-Marc

    Comment

    • PyPK

      #3
      Re: calculate system disk space

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

      Comment

      • Heiko Wundram

        #4
        Re: calculate system disk space

        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.

        For explanations, see:


        (entries for stat() and walk())

        --- Heiko.

        Comment

        • rainbow.cougar@gmail.com

          #5
          Re: calculate system disk space

          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(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

          file_sys,disc_s ize,disc_avail, disc_cap_pct,in odes,inodes_pct ,mount=df_list. split()
          mount_list.appe nd(mount)

          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

          file_sys,disc_s ize,disc_used,d isc_avail,disc_ cap_pct,mount=d f_list.split()
          mount_list.appe nd(mount)

          return (mount_list)



          """
          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

          rawnum=string.r eplace(rawnum,' ,','')
          free_space=floa t(rawnum)*multi plier
          #print
          (0,int(free_spa ce)+int(total_u sed),int(total_ used))
          return
          (0,int(free_spa ce)+int(total_u sed),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.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

          file_sys,disc_s ize,disc_avail, disc_cap_pct,in odes,inodes_pct ,mount=df_list. split()
          return(0,int(di sc_size),int(di sc_size)-int(disc_avail) )

          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

          file_sys,disc_s ize,disc_used,d isc_avail,disc_ cap_pct,mount=d f_list.split()
          #mount_list.app end(mount)
          return(0,int(di sc_size),int(di sc_used))




          if __name__ == '__main__':
          print(mount_lis t())

          Comment

          • Heiko Wundram

            #6
            Re: calculate system disk space

            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.

            --- Heiko.

            Comment

            • Neil Hodgson

              #7
              Re: calculate system disk space

              Heiko Wundram:
              [color=blue]
              > Under Windows you don't have sparse files though, so there
              > are no fields ...[/color]

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

              Neil

              Comment

              Working...