0

In my penultimate (next to last) 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"

martineau
  • 112,593
  • 23
  • 157
  • 280
Àngel Ba
  • 361
  • 2
  • 9

2 Answers2

2

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'))
Jon Clements
  • 132,101
  • 31
  • 237
  • 267
1

Using ast.literal_eval and glob.glob:

import ast
import glob
print([ast.literal_eval(open(filename).readlines()[-1]) for filename in [("desired_directory/chr*.txt"])
Ramchandra Apte
  • 3,975
  • 2
  • 23
  • 43