1,105,578 Community Members

Stop reading file at certain point

Member Avatar
pyprog
Light Poster
31 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to write a program that prints every line of the file until a certain string is found. The file is a plain text file. I put the sample context at the top of the code. But the function gets stuck in an infinite loop. I don't want to use a break statement so I have no idea how to fix it.

'''Where he at
There he go
Peanut butter jelly
Do the peanut butter jelly, peanut butter jelly
Peanut butter jelly with a baseball bat'''

def limit_read(filename):
    string = 'baseball'
    for line in f:
        while line.find(string) == -1:
            print line
Member Avatar
IsharaComix
Junior Poster in Training
98 posts since Feb 2010
Reputation Points: 97 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

You shouldn't be using a while loop inside of the for loop. That will just result in the same line being parsed over and over again. Look carefully, and you'll see that because line doesn't change, the condition can never change, so the loop will never end or it will never start.

def limit_read(filename):
    string = "baseball"
    finished = False
    f = file(filename)
    for line in f:
        if not finished:
            if line.find(string) == -1:
                print line
            else:
                finished = True

The proper way is really to use a break statement so we don't waste time reading over the rest of the file. This is exactly the situation that it's used for. I'm not sure why you don't want to use it. Here's how nice the code looks with the break statement.

def limit_read(filename):
    string = "baseball"
    f = file(filename)
    for line in f:
        if line.find(string) == -1:
            print line
        else:
            break
Member Avatar
pyprog
Light Poster
31 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry, forgot to paste open and close file

'''Where he at
There he go
Peanut butter jelly
Do the peanut butter jelly, peanut butter jelly
Peanut butter jelly with a baseball bat'''

def limit_read(filename):
    f = open(filename)
    string = 'baseball'
    for line in f:
        while line.find(string) == -1:
            print line
    f.close()

if __name__ == "__main__":
    filename = 'song.txt'
Member Avatar
pyprog
Light Poster
31 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What is the variable finished for?

Member Avatar
IsharaComix
Junior Poster in Training
98 posts since Feb 2010
Reputation Points: 97 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

Because you don't want to print anything that occurs after the string in question, we need to have a storage variable that lets us know that we're done printing data in the file. Ideally, the loop would end when finished was true, but you asked for a breakless loop, so this is the best way that preserves your original structure.

Question Answered as of 4 Years Ago by IsharaComix
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article