Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    How to read a specific number of lines from file?

    Scheduled Pinned Locked Moved MicroPython
    5 Posts 2 Posters 77 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      railmonitor
      last edited by

      Hi.

      Having a little trouble with figuring this out.

      Lets say i have a .txt file, where i have written some string followed by a newline character, so i have a lot of lines in my file.

      Now i would like to read, lets say, 100 lines of my file, but i also want to use line.strip() or the like to get rid of the newline character \n. Preferably i want to read the first 100 lines, then the next 100 lines and so on to avoid any memory exceptions and such.

      Searching documentation on google, i found that there is a readlines() method, which could take an argument that would specify how many bytes of the file to read, but always finishing a line. This is exactly what i need, then i would just have to find out how many bytes that the amount of lines i want is. However when i try:

      f.readlines(100)
      

      i am getting the following error:

      TypeError: function takes 1 positional arguments but 2 were given
      

      Can someone please explain what i am doing wrong?

      Full example:

      sd = SD()
      os.mount(sd, '/sd')
      
      f = open('/sd/records/0', 'r')
      
      lines_read = f.readlines(100)
      
      print(lines_read)
      
      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @railmonitor
        last edited by

        @railmonitor readlines() reads all lines from a file. MicroPython does not support the optional argument. And even if, it is not the number of lines, but the number of bytes.
        You can use readline() to read a single line and repeat that until you get the request number of lines.

        1 Reply Last reply Reply Quote 0
        • R Offline
          railmonitor
          last edited by

          @robert-hh Thank you for replying.

          Ok, so then i can do something like :

          with f as myfile:
              head = [next(myfile).strip() for x in range(5)]
          print(head)
          

          To get the first 5 lines in the file. But the thing is, i want to be able to repeatedly be able to call a function, that gets me the first N number of lines, then call it again to get the next N number of lines... But calling the above code several times gives the same 5 lines at every instance. Any ideas as to how to get the next 5 lines afterwards? Can i delete single lines after i'm done reading them?

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @railmonitor
            last edited by

            @railmonitor This happens because you re-open the file in each call. So you either have to keep the file open, or use tell() and seek() to recall the last position in the file con continue reading from that place the next time. Looks like you could use a tiny class for that.

            R 1 Reply Last reply Reply Quote 0
            • R Offline
              railmonitor @robert-hh
              last edited by railmonitor

              @robert-hh Great thanks! Hadn't found those methods myself, only been coding in Python for a few months!

              For anyone else looking for a similar solution, this should do the trick:

              sd = SD()
              os.mount(sd, '/sd')
              f = open('filepath')
              
              lines = []
              stepsize = 5
              offset = 0
              
              def read_lines():
                  global offset
                  global lines
                  f.seek(offset)
                  for x in range(stepsize):
                      lines.append(f.readline().strip())
                  print(lines)
                  offset = f.tell()
                  print(offset)
                  lines = []
              

              Then you can close (and delete if you want) the file when done calling the method.

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom