By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Odd behavior writing file...

P: 1
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
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert Mod 2.5K+
P: 2,851
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
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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.