473,406 Members | 2,705 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.

Reading a file in an inner loop [solved]

my computer doesn't like this and hangs after creating a zero byte temp file. thanks for the help in advance

Expand|Select|Wrap|Line Numbers
  1. def Del_Instr(from_file, instr):
  2.     "del an instrument from a file dangerous indeed"
  3.     infile = open(from_file, 'r')
  4.     outfile = open('temp.tmp', 'w')
  5.     instr_num = str(instr)
  6.     for line in infile:
  7.         if 'instr' in line:
  8.            if instr_num in line:
  9.               while not 'endin' in line:
  10.                   pass                
  11.            outfile.write(line)         
  12.     close(infile)
  13.     remove(from_file)
  14.     rename('temp.tmp', from_file) 
  15.  
Oct 12 '06 #1
3 1337
bartonc
6,596 Expert 4TB
creating an inner loop IS what you want to do, but
Expand|Select|Wrap|Line Numbers
  1.               while not 'endin' in line:
  2.                   pass                
  3.  
hangs because you never read any new lines.

I still recommend this inside the if block (you have found what you want copied)
Expand|Select|Wrap|Line Numbers
  1. outfile.write(line)
  2. try:
  3.     while True:
  4.         line = infile.next()
  5.         outfile.write(line)
  6.         if 'endin' in line:
  7.             break
  8. except StopIteration:
  9.     print 'never found endin'
  10.  
This way you get the first line written to the outfile plus the line that contains 'endin'.
Oct 12 '06 #2
bartonc
6,596 Expert 4TB
It's probably better to collect all the lines that you want into a list:

out_lines = []
...
out_lines.append(line)
...

if out_lines:
# open, write and close the outfile with whatever name you want.
Oct 12 '06 #3
I probily should of realized the infinite loop.. I will give this a try thanks alot for the help

creating an inner loop IS what you want to do, but
Expand|Select|Wrap|Line Numbers
  1.               while not 'endin' in line:
  2.                   pass                
  3.  
hangs because you never read any new lines.

I still recommend this inside the if block (you have found what you want copied)
Expand|Select|Wrap|Line Numbers
  1. outfile.write(line)
  2. try:
  3.     while True:
  4.         line = infile.next()
  5.         outfile.write(line)
  6.         if 'endin' in line:
  7.             break
  8. except StopIteration:
  9.     print 'never found endin'
  10.  
This way you get the first line written to the outfile plus the line that contains 'endin'.
Oct 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
13
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own...
5
by: Jay | last post by:
I want to store various text settings in a configuration file, that I will read into C# and make use of. Here's a simplified example of the file: #comment loop(var=1:5){ deviceundertest ...
5
by: GeekBoy | last post by:
Thanks for the help obnoxious. Reading in the rest of the line with "getline" worked great and now the averages work. Now for the new problem. Program on first run works flawlessly. Next runs...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
13
by: cmdolcet69 | last post by:
I have this code below that will open a created file and read the file to the end. However if my file has values 200,300,400 seperated by commas how can i read in each seperate value and place it...
7
by: tackleberi | last post by:
hi, im having some trouble reading a file into java and then storing it in an array here the code i have so far: import java.io.FileNotFoundException; import java.io.FileReader; import...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.