Mental block with os.walk - want to process a file not the filename string
Question: Trying to implement a little script to move the older log files out of apache
(actually using a simple bash script to do this in 'real life' - this is just
an exercise to practice using Python). I'm getting the filename as a string as
the variable f, but I want this to actually be a file when i pass it to
self.processFile(root, f, age, inString).

I tried opening the actual file a few different ways, but I'm missing the
target, and end up getting an error, a path that doesn't always seem to be
correct, or just a string. I'll blame it on the late night, but I'm blanking
on the best way to open f as a file right before passing to self.processFile
(where it will be gzipped). Usually its something very simple that i'm
missing, so I have to assume that's the case here. I'd appreciate any
constructive advice/direction.

    
    
     """recursive walk through /usr/local/apache2.2/logs"""
        for root, dirs, files in os.walk(basedir):
                for f in files:
                    m=self.fileFormatRegex.match(f)
                    if m:
                        if (('access_log.' in f) or
                            ('error.' in f) or
                            ('access.' in f) or
                            ('error_log.' in f) or
                            ('mod_jk.log.' in f)):
                            #This is where i'd like to open the file using the filename f
                            self.processFile(root, f, age, inString)
    


Answer: Use `os.path.abspath`:

    
    
    self.processFile(root, open(os.path.abspath(f)), age, inString)
    

Like so:

    
    
    import os
    for root, dirs, files in os.walk(basedir):
        for f in files:
            m=self.fileFormatRegex.match(f)
            if m:
                if (set('access_log.', 'error.', 'access.', 'error_log.','mod_jk.log.').intersection(set(f))):
                    self.processFile(root, open(os.path.abspath(f)), age, inString)
    

Or `os.path.join`:

    
    
    import os
    for root, dirs, files in os.walk(basedir):
        for f in files:
            m=self.fileFormatRegex.match(f)
            if m:
                if (set('access_log.', 'error.', 'access.', 'error_log.','mod_jk.log.').intersection(set(f))):
                 self.processFile(root, open(os.path.join(r"/", root, f)), age, inString)
                 # Sometimes the leading / isnt necessary, like this:
                 # self.processFile(root, open(os.path.join(root, f)), age, inString)
    

More about [`os.path`](http://docs.python.org/library/os.path.html)

* * *

Yet another way using `file()` instead of `open()` (does almost the same thing
as open):

    
    
    self.processFile(root, file(os.path.join(root, f), "r"), age, inString)
    self.processFile(root, file(os.path.abspath(f), "r+"), age, inString)
    

