[Tutor] manipulating a file

Jacob S. keridee at jayco.net
Wed Feb 9 05:12:46 CET 2005

> 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()

Okay, so how about...

a = '/var/log/httpd-access.log'
srcfile = open(a,'r')
dstfile = open(a+'.bak,'w')
for x in srcfile:
        print >> dstfile, x

1) Put filename in seperate variable because I'm lazy and didn't want to 
type it twice.
2) Implemented the file iteration technique.
3) Odd alternative way for line check. Remeber, there's more than one way to 
do it!
4) Implemented the cool redirection of print statement

Not bad, what do you thing, Reed?

Does anybody have any input on alternative length check technique? Will it 
be less efficient than just using len()?


More information about the Tutor mailing list