[Tutor] manipulating a file

Reed L. O'Brien reed at intersiege.com
Mon Feb 7 07:58:57 CET 2005


I want to read the httpd-access.log and remove any oversized log records

I quickly tossed this script together.  I manually mv-ed log to log.bak 
and touched a new logfile.

running the following with print i uncommented does print each line to 
stdout.  but it doesn't write to the appropriate file...

a) what am I missing?
b) is there a less expensive way to do it?
c) I originally wanted to delete lines over 2085 in length but couldn't 
find a way to do that... did I miss it?

Thanks

#!/usr/local/bin/python

import os

srcfile = open('/var/log/httpd-access.log.bak', 'r')
dstfile = open('/var/log/httpd-access.log', 'w')
while 1:
     lines = srcfile.readlines()
     if not lines: break
#    print lines
     for i in lines:
         if len(i) < 2086:
             #print i
             dstfile.write(i)

srcfile.close()
dstfile.close()



More information about the Tutor mailing list