Loop of a text file showing first 20 lines?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huud

    Loop of a text file showing first 20 lines?

    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?
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    @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!

    Comment

    • huud

      #3
      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*a

      Comment

      • huud again

        #4
        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 :D

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Use an inner loop which does story.readline( ) once, and prints each individual line independently.

          Then your outer loop can wait patiently for the next user input, then it releases the inner loop, etc....

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            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 join
            Code:
            '\n'.join(thelist)
            Call raw_input() which pauses processing

            Now for the except part of the try/except block (except StopIteration:)

            If there is anything in the list, print it

            Break from the loop

            Comment

            Working...