How can I read large files that complete themselves

Viewed 24

So, I need to apply a function to each file available online here. This file is just a large number divided in different files. So, I need not only analyze each file, but, when the file finish, I need to read, for example, the 100 last lines of the file and the 100 first lines of the second file, before analyze the second file next. How can I do that? I have a code to analyze one file:

def prime_palindromo(number, number_size):
    k = number_size
    list_number = list(str(number))
    list_pal = []
    for j, item in enumerate(list_number):
        iterate_lst = list_number[j:k]
        if iterate_lst == iterate_lst[::-1] and len(iterate_lst) == number_size and sympy.isprime(int(''.join(iterate_lst))):
            return(f"The number {int(''.join(iterate_lst))} is the first palindromic prime number with {number_size} digits in PI decimal.")
            break
        k += 1

###################################################################################################################

number_size = 21

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

with open(filename, 'wb') as fp:
    while True:
        chunk = response.read() # Lendo um pedaço dos dados
        palindrom_number = prime_palindromo(chunk, number_size)
        if palindrom_number:
            print(palindrom_number)# Aplicando a função somente nos pedaços dos dados
        if not chunk:
            break
0 Answers
Related