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

One thought on “Using Python and os.walk to Locate the Firefox Cache

  1. Change

    for root, dirs, files in os.walk(home):
    for x in files:
    if x == searchFile:
    cacheFolder = root
    print cacheFolder

    to:

    for root, dirs, files in os.walk(home):
    if searchFile in files:
    cacheFolder = root
    print cacheFolder

    Although I prefer the name last_found to cacheFolder.
    Also,
    if cacheFolder is None:
    print “Cache folder not found under %r.” % home
    is, I think, a more Pythonic final statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>