Get Disk/Flash Drive Info (Windows)

vegaseat 4 Tallied Votes 3K Views Share

This program uses the Python win32 extension module to get information on disk or flash drives for Windows machines. Specify the drive with its letter. If no drive letter is specified, the current drive is applied.

scru commented: Nice post +4
Gribouillis commented: Nice +2
# get disk/flash drive information on Windows
# using the win32 extension module from:
# http://sourceforge.net/projects/pywin32/files/
# tested on Windows XP with Python25 and Python31 by vegaseat

import win32file
import os

def get_drivestats(drive=None):
    '''
    drive for instance 'C'
    returns total_space, free_space and drive letter
    '''
    # if no drive given, pick the current working directory's drive
    if drive == None:
        drive = os.path.splitdrive(os.getcwd())[0].rstrip(':')
    sectPerCluster, bytesPerSector, freeClusters, totalClusters = \
        win32file.GetDiskFreeSpace(drive + ":\\")
    total_space = totalClusters*sectPerCluster*bytesPerSector
    free_space = freeClusters*sectPerCluster*bytesPerSector
    return total_space, free_space, drive

# use default drive (current drive)
# or specify drive for instance C --> get_drivestats('C')
total_space, free_space, drive = get_drivestats()

print( "Drive = %s" % drive )
print( "total_space = %d bytes" % total_space )
print( "free_space  = %d bytes" % free_space )
print( "used_space  = %d bytes" % (total_space - free_space) )

print( '-'*40 )
mb = float(1024 * 1024)  # float() needed for Python2

print( "Drive = %s" % drive )
print( "total_space = %0.2f Mb" % (total_space/mb) )
print( "free_space  = %0.2f Mb" % (free_space/mb) )
print( "used_space  = %0.2f Mb" % ((total_space - free_space)/mb) )

print( '-'*40 )
gb = float(1024 * 1024 * 1024)

print( "Drive = %s" % drive )
print( "total_space = %0.2f Gb" % (total_space/gb) )
print( "free_space  = %0.2f Gb" % (free_space/gb) )
print( "used_space  = %0.2f Gb" % ((total_space - free_space)/gb) )

"""possible output -->
Drive = D
total_space = 59674370048 bytes
free_space  = 48973877248 bytes
used_space  = 10700492800 bytes
----------------------------------------
Drive = D
total_space = 56909.91 Mb
free_space  = 46705.13 Mb
used_space  = 10204.79 Mb
----------------------------------------
Drive = D
total_space = 55.58 Gb
free_space  = 45.61 Gb
used_space  = 9.97 Gb
"""
Archenemie 2 Junior Poster

Traceback (most recent call last):
File "C:/Python26/Disk Space of Drives", line 6, in <module>
import win32file
ImportError: No module named win32file

Stefano Mtangoo 455 Senior Poster

You should have read the intro as well as comments before copying the code. To help you it says: This program uses the Python win32 extension module

Gribouillis 1,391 Programming Explorer Team Colleague

A similar utility for linux users, using os.stavfs (tested with py 2.6 and 3.1)

import os, sys

kilo = 1024.0
mega = kilo * kilo
giga = kilo * mega

def fullpath(location):
    p = os.path.expandvars(location)
    p = os.path.expanduser(p)
    return os.path.abspath(os.path.normpath(p))

def hardpath(location):
    p = fullpath(location)
    L = []
    while True:
        while os.path.islink(p):
            p = fullpath(os.readlink(p))
        p, name = os.path.split(p)
        if not name:
            L.append(p)
            return fullpath(os.path.join(*reversed(L)))
        else:
            L.append(name)

def mount_point(location):
    p = hardpath(location)
    while not os.path.ismount(p):
        p = os.path.dirname(p)
    return p

def print_disk_info(location):
    location = sys.argv[-1]
    mpoint = mount_point(location)
    stv = os.statvfs(mpoint)

    bn, bf, ba = stv.f_blocks, stv.f_bfree, stv.f_bavail
    ino, ifr, ifa = stv.f_files, stv.f_ffree, stv.f_favail
    print("Mount point: %s" % mpoint)
    print("Blocks: %s total, %s free, %s available for non root." % (bn, bf, ba))
    print("Inodes: %s total, %s free, %s available for non root." % (ino, ifr, ifa))
    bsize = stv.f_bsize
    print("Block size: %d" % bsize)
    print("Space (MB): %.f total, %.f free, %.f available" % tuple(x * bsize/mega for x in (bn, bf, ba)))
    print("Space (GB): %.1f total, %.1f free, %.1f available" % tuple(x * bsize/giga for x in (bn, bf, ba)))


if __name__ == "__main__":
    print_disk_info(os.getcwd())
Gribouillis 1,391 Programming Explorer Team Colleague

There are 2 small bugs in the previous post: remove the line location = sys.argv[-1] in function print_disk_info . Also in hardpath() , the call os.readlink(p) should be replaced by _readlink(p) where _readlink is the function

def _readlink(p):
    ln = os.readlink(p)
    if os.path.isabs(ln):
        return ln
    else:
        p, name = os.path.split(p)
        return os.path.join(p, ln)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.