Deleting lines already read in a file

Viewed 16

I have a huge .txt file hosted online. I want to read each chunk of this file and delete that chunk when I'm done. So, if I have a file number_pi.txt that contains, for example, this:

3.141592653589793238462643383279502884197169399

64428810975665933446128475648233786783165827636

I'm trying to think in a code that, when I read the first chunk, he delete it, remaining only this:

64428810975665933446128475648233786783165827636

I hope this code would do it:

from urllib.request import urlopen


url = 'https://archive.org/download/pi_dec_1t/pi_dec_1t_01.zip/pi_dec_1t_01.txt'
    response = urlopen(url)
    CHUNK = 1000000
    filename = 'pi_dec_1t_01.txt'

    print(f"The file processed is {filename}.")
    with open(filename, 'wb') as fp:
        while True:
            chunk = response.read(CHUNK) # Lendo um pedaço dos dados
            apply_some_function(chunk)
            if not chunk:
                break
            fp.seek(0)
            fp.truncate() # Excluindo pedaço dos dados carregados

I expect that this code open the file on url, read the file by chunks, delete the chunk and move on to the next one. But, I don't know how to validate this, nor if my understanding is right.

0 Answers
Related