I have some simple code I wrote to open a large log file with 39 extra characters starting each line that I want to strip out. Then I just want to rewrite the lines in a new file without those 39 preceding characters per line.
Expand|Select|Wrap|Line Numbers
- output = open('newlogfile', 'w')
- for line in open('ClientLog_072507_17302.log','r'):
- clipline = line[39:]
- output.write(clipline)
- output.close
- output = open('testlogfile', 'w')
So, this code actually works for my purposes, but I'd like to know what I did wrong for it not to work without opening another bogus file. I added the output.close even though I thought it wasn't absolutely necessary for simple tasks like this. I also originally had the string handling code in the same line as the output.write but I broke it up into two lines to see if I was making some mistake with the string.
The logfile to read is in the same directory as the program so I didn't include a path.
Any help anyone can provide in pointing out what I might have gotten wrong in the output file handling would be greatly appreciated. Since I have the cludgy workaround, there is no urgency in this. But I'd like to know the right way to do this. Thanks