471,356 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Odd behavior writing file...

I'm guessing I'm missing something obvious here, but I have searched around and re-read my Python books and references on simple file writing and I can't see the answer. I am running this in Windows XP in IDLE.

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
  1. output = open('newlogfile', 'w')
  2. for line in open('ClientLog_072507_17302.log','r'):
  3.     clipline = line[39:]
  4.     output.write(clipline)
  5. output.close
  6. output = open('testlogfile', 'w')
  7.  
As you can see I am opening another output logfile at the end. When I ran the code without that last line it never seemed to write to the first output file. I could print the results correctly in the line just prior to the output.write line, but the output file size was always zero. As soon as I opened a new bogus file with the last line, it wrote the first file with all my data.

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
Aug 10 '07 #1
4 1665
bvdet
2,851 Expert Mod 2GB
I'm guessing I'm missing something obvious here, but I have searched around and re-read my Python books and references on simple file writing and I can't see the answer. I am running this in Windows XP in IDLE.

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
  1. output = open('newlogfile', 'w')
  2. for line in open('ClientLog_072507_17302.log','r'):
  3.     clipline = line[39:]
  4.     output.write(clipline)
  5. output.close
  6. output = open('testlogfile', 'w')
  7.  
As you can see I am opening another output logfile at the end. When I ran the code without that last line it never seemed to write to the first output file. I could print the results correctly in the line just prior to the output.write line, but the output file size was always zero. As soon as I opened a new bogus file with the last line, it wrote the first file with all my data.

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
The file object must be closed properly to flush the data to disk:
Expand|Select|Wrap|Line Numbers
  1. output.close()
file.close() is a file method.
Aug 10 '07 #2
bvdet
2,851 Expert Mod 2GB
If your log file is not huge, you can do something like this which should be more efficient:
Expand|Select|Wrap|Line Numbers
  1. outList = [line[39:] for line in open('ClientLog_072507_17302.log','r')]
  2. output = open('newlogfile', 'w')
  3. output.write(''.join(outList))
  4. output.close()
Aug 10 '07 #3
bartonc
6,596 Expert 4TB
If your log file is not huge, you can do something like this which should be more efficient:
Expand|Select|Wrap|Line Numbers
  1. outList = [line[39:] for line in open('ClientLog_072507_17302.log','r')]
  2. output = open('newlogfile', 'w')
  3. output.write(''.join(outList))
  4. output.close()
FingerDemon: You left off the parentheses on line 5. It should read:
Expand|Select|Wrap|Line Numbers
  1. output.close()
BVDet: What's wrong with the writelines() method?
Expand|Select|Wrap|Line Numbers
  1. output.writelines(outList)
Aug 10 '07 #4
bvdet
2,851 Expert Mod 2GB
FingerDemon: You left off the parentheses on line 5. It should read:
Expand|Select|Wrap|Line Numbers
  1. output.close()
BVDet: What's wrong with the writelines() method?
Expand|Select|Wrap|Line Numbers
  1. output.writelines(outList)
You are correct, writelines() would be better in this case. join() may be useful when a '\n' is required between lines.
Aug 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Aaron Bertrand - MVP | last post: by
5 posts views Thread by Danny Anderson | last post: by
6 posts views Thread by Samuel M. Smith | last post: by
3 posts views Thread by Chuck Renner | last post: by
111 posts views Thread by Nate | last post: by
7 posts views Thread by Michael Castleton | last post: by
28 posts views Thread by v4vijayakumar | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.