473,228 Members | 1,814 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,228 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 1778
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';
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.