Using Python and os.walk to Locate the Firefox Cache

My series of Beazley-related Python lessons picks up again here. This is actually a small part of a bigger assignment, which was to decode the files within a user's Firefox cache (coming later), but this nice little script can be handy to find about anything on your system with Python.

Every Firefox cache contains a file called "_CACHE_MAP_", so this script will search for that. You can run the script by passing a parameter in for the folder you want to begin your search tree with. This might be a User folder, or even just root ('/'). Just remember that the closer you get to root, the more there is to search, and the longer this will take. For example, you might run the script like this:

 python findcache.py /Users/username

And it will spit out the paths it finds the _CACHE_MAP_ file in. On my system, this took just under 6 seconds and located 4 cache folders within my user folder (a test installation of Komodo created one, and besides the "real" one I actually use, I also had two "fake" ones for study).

If You're So Inclined

Try writing the script! Here's the man page on os.walk. The solution is very short. Here's my version:

#!/usr/bin/env python
# encoding: utf-8
"""
findcache.py
Created by Matt Mayes on 2008-02-18.
Finds the Firefox cache directory when given a home directory
"""
 
import sys
import os
 
searchFile = '_CACHE_MAP_'
cacheFolder = None
 
try:
    home = sys.argv[1]
except IndexError,e:
    print "Please enter your home directory: (no trailing slash) "
    home = raw_input()
 
for root, dirs, files in os.walk(home):
    for x in files:
        if x == searchFile:
            cacheFolder = root
            print cacheFolder
 
if cacheFolder == None: print "Cache folder not found under that directory."
This entry was posted in Python. Bookmark the permalink.

One Response to "Using Python and os.walk to Locate the Firefox Cache"

Leave a reply