How can I code a loop of a text file showing it's first 20 lines and after pressing enter it shows the next 20 lines and so on?
Loop of a text file showing first 20 lines?
Collapse
X
-
huudTags: None -
@huud,
Try putting something together and posting it, if you're having troubles with the code. We'll gladly help.
To get off square one with python, go try one of the tutorials that are all over the 'Net. It'll get you a lot farther than asking for code chunks.
Luck! -
huud
OK, I've got it to show the file's first line 20 times over and over again.
Code:f = open("story.txt", "r") a = f.readlines() while True: print 20*aComment
-
huud again
reply
I've got it to show the first 20 lines of a text file over and over.
[code] story = open("11.txt", "r")
a = story.readline( )
b = story.readline( )
c = story.readline( )
d = story.readline( )
e = story.readline( )
f = story.readline( )
g = story.readline( )
h = story.readline( )
i = story.readline( )
j = story.readline( )
k = story.readline( )
l = story.readline( )
m = story.readline( )
n = story.readline( )
o = story.readline( )
p = story.readline( )
q = story.readline( )
r = story.readline( )
s = story.readline( )
t = story.readline( )
all = a+b+c+d+e+f+g+h +i+j+k+l+m+n+o+ p+q+r+s+t
while True:
print all
[code]
There's gotta be a beter way to do this :DComment
-
Here's some pseudocode:
Open the file, assign to an identifier
Start a while loop, maybe while True:
Initialize an empty list
I am using file method next() which raises a StopIteration exception when reaching the end of a file, so start a try/except block
Start a for loop over a range of 20
Append the next line of the file to the list using file method next(), end of for loop
Print the list using string method joinCall raw_input() which pauses processingCode:'\n'.join(thelist)
Now for the except part of the try/except block (except StopIteration:)
If there is anything in the list, print it
Break from the loopComment
Comment