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 i开发者_如何转开发ts 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)
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
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)
base = "/some/path"
for root, dirs, files in os.walk(base):
for f in files:
thefile = file(os.path.join(root, f))
You'll have to join the root
argument to each of the files
argument to get a path to the actual file.
精彩评论