473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1783
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

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

Similar topics

7
by: Aaron Bertrand - MVP | last post by:
Based on a complaint that one of my articles just links to the MS documentation, I'm writing a tutorial on FileSystemObject. However I'm currently getting stalled by this bug. Environment:...
5
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the...
3
by: sstreaker | last post by:
I'm serving an MP3 file via a BinaryWrite from an ASPX file. If I have Content-Disposition set to "inline", WMP opens as expected but hits my ASPX page 3 times and then finally play the file. ...
4
by: welch | last post by:
while taking some rough disk performance measures on windows machines, and snooping with FileMon, i've noticed some odd behavior here's the little nul-writer i'm running: def writeTest(nBlocks,...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
3
by: Chuck Renner | last post by:
Please help! This MIGHT even be a bug in PHP! I'll provide version numbers and site specific information (browser, OS, and kernel versions) if others cannot reproduce this problem. I'm...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
7
by: Michael Castleton | last post by:
When I open a csv or txt file with: infile = open(sys.argv,'rb').readlines() or infile = open(sys.argv,'rb').read() and then look at the first few lines of the file there is a carriage return...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.