Àngel Ba Àngel Ba - 1 year ago 77
Python Question

How to read an output line containing a list of integers produced

In my penultimate line from all the files in a specific directory, I have a list of integers like this:

[142356, 12436546, 131434645, 56464]

I would like to read this penultimate line from all the files from that specific directory, and using it again as a list in my new python script containing the same values, of course. So I will be able to process again these lists.

All the text filenames start by "chr" and finish by ".txt"

Answer Source

You can use the following - iglob to match the filenames, literal_eval to parse the data as a list, and a deque to efficiently get the last two lines of a file:

from collections import deque
from glob import iglob
import ast

def get_lists(pattern):
    for filename in iglob(pattern):
        with open(filename) as fin:
            penultimate = deque(fin, 2)[0]
            yield ast.literal_eval(penultimate)

data = list(get_lists('chr*.txt'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download