views:

73

answers:

2

This is the code i have at the moment

print "Please input the filename:"
n = raw_input()
f = open(n,"r")
x = 0
for line in f.readlines():
    print line
    x+=1
    if x % 20 == 0:
        break
        q = raw_input()
        if q == "":
            x+= 20
        continue

Things the program should do: 1) Ask for a filename 2) Read the file 3) Print the first 20 lines of the file 4) Stop working after the first 20 lines and wait for an Enter keypress 5) If Enter is pressed show the next 20 lines of the file (20->40 and so on)

Current problem: The loop doesn't restart, it only shows the first 20 lines and then stops working.

+1  A: 

Basically, you want to pause every 20 lines. Currently, you're breaking out of your loop after the first 20 lines.

for line in f:
    print line
    x += 1
    if x % 20 == 0:
        raw_input("Hit enter")

should suffice.

Ivo van der Wijk
Thanks for the fast response (and sorry about the bad formatting, still new to this :S)...I changed it to what you said, but now it only prints the file out one maybe two lines at a time for some reason.
Mr Wotan
change `if x % 20` to `if x % 20 == 0`
robert
@robert good point, thanks. Fixed!
Ivo van der Wijk
A: 
filename = raw_input("Please enter the file name: ")
with open(filename) as f:
    lines = f.readlines()
    for i in xrange(0, len(lines), 20):
        print lines[i:i+20]
        raw_input("Press Enter for more")

This reads the whole file into memory so you might not want to use slices if it's a huge file. But if it's a huge file, you're unlikely to want to step through it twenty lines at a time ;)

The major change is that we are opening the file using a with statement. This is much nicer than using open/close and guarantees that the file will always be closed.

in the code that you posted, when you executed break, you were exiting your look. break leaves the loop so you only want to use it for that purpose. just calling a blocking operation likeraw_input` suffices if you just need to pause execution for some reason (like waiting on the user).

also, the continue is entirely unnecessary. At the end of the body of a loop, it has no choice but to continue

What were you thinking with the linesif q == "": x += 20? first off, it should just be if not q: x += 20 (empty string (like empty lists/dicts/tuples) evaluates to False) and second, this would skip the next 20 lines. Is this a requirement that you haven't shared?

aaronasterling
Thanks for your response. Your code does the trick - the program asks for a filename, opens it, reads the lines, then shows the file 20 lines at a time.
Mr Wotan
My code is bad because I just started learning python.
Mr Wotan
@Mr Wotan. Don't worry about bad code, my code could still use a _lot_ of work. It's part of the learning process. A good tip is to only do __one__ thing that you don't understand at a time. That way, you can come to understand it. So just stick a `break` statement in the loop __or__ a `continue` statement to see what they do. But not both or you'll have no good way to know what's messing it up. In addition to that, You of course want to get good at reading the literature.
aaronasterling