Tuesday, September 6, 2011

Finding Disk Size

Ever need to know how much space is left on a disk drive, well I did when creating map books for large areas. import os 
import platform
import ctypes 
def get_free_space(folder):
   """ Return folder/drive free space (in bytes) """ 
   if platform.system() == 'Windows': 
      free_bytes = ctypes.c_ulonglong(0)
      ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) return free_bytes.value 
   else: 
      return os.statvfs(folder).f_bfree 
if __name__ == "__main__":
   path = (r"network drive") 
   print get_free_space(path) 

This prints the free space as bytes. It is cross platform and will work on both Linux,MaxOS and Windows. Enjoy

No comments: