How to read a specific number of lines from file?



  • 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-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.



  • @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.



  • @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?



  • @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.


Log in to reply
 

Pycom on Twitter