Follow me on Twitter RSS FEED

Python Project: Recursive File Scanner

My harddrive was filling up, so I wanted to find the biggest files on my computer. I thought python would be the perfect language to do this, so I wrote a little script. The script starts in drive C:\, and recursively scans through every directory and file on your computer. If any file is larger than one Gigabyte, then it is put in a list. After every file has been scanned, then the program prints out the list of files larger than a Gigabyte. This is very useful for finding big files to clear your hard drive. Here is the code:


    import os


    def listdir(path):
        good = []
        try:
            files = os.listdir(path)
            count = 0
            for i in files:
                filepath = os.path.join(path,files[count])
                byte = os.path.getsize(filepath)
                gigabyte = byte / 1073741824
                if(gigabyte >= 1):
                    good.append(filepath + "  --  " + str(gigabyte) + "GB")
                if(os.path.isdir(filepath)):
                    bbt = 0
                    tempt = listdir(filepath)
                    for p in tempt:
                        good.append(tempt[bbt])
                        bbt += 1
                count += 1
        except WindowsError, e:
            print "Windows Error:"+str(e)
            print " -- Continuing..."
        return good


    lists = listdir("C:\\")
    count = 0
    for i in lists:
        print lists[count]
        count += 1


I am, as we [well actually just me] speak, moving quite a few GB to our MyBook.

2 comments:

Anonymous said...

Cool, did you compile and upload it? If you do, you might want it to list all files over a user defined amount, say 100MB and list largest files first. ^_^ ~M

Adam Gaskins said...

You can't compile python [unless you use a 3rd party software like py2exe]. It uses an interpreter.

And if you want to customize how big, then edit this line and change 1 to however many gigbytes you want [decimals are OK]:

if(gigabyte >= 1):

Post a Comment