0

I want to use the following code inside a function on 50 files in the same directory.

with open(files) as f:
     my_list = [int(i) for line in f for i in line.split()]

I tried

with open(file1) as a, open(file2) as b, ... open(file50) as c:
    list1 = [int(i) for line in a for i in line.split()]
    list2 = [int(i) for line in b for i in line.split()]
    ...
    list50 = [int(i) for line in c for i in line.split()]
    my_list = list1 + list2 + ... list50

And thus combining the contents of 50 files into the same array. But that is way too long and complicated and it also doesn't work. I know there is a much easier way to do this, I just don't know how to do that yet. Any help is much appreciated.

Sekou
  • 35
  • 6

2 Answers2

1

You can use fileinput.input for this:

import fileinput


items = []
for line in fileinput.input(files):
    items.extend(int(x) for x in line.split())

In Python 3.2+ it can also be used a context-manager:

with fileinput.input(files=files) as f:
    for line in f:
        items.extend(int(x) for x in line.split())
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • one of the cases where `items.extend(int(x) for x in line.split())` can be replaced by `items.extend(map(int,line.split()))` – Jean-François Fabre Jun 18 '17 at 19:35
  • @Ashwini How do I do that for 50 files though? – Sekou Jun 18 '17 at 19:39
  • @Sekou I assume you have a list of files already, you just need to pass the list to `fileinput.input`([use `os.listdir()` to get a list of all files in a directory](https://stackoverflow.com/q/3207219/846892)). – Ashwini Chaudhary Jun 18 '17 at 19:44
1

Maybe I'm completely missing the point, but you don't need all the files open at the same time, so what's wrong with a loop and opening the files one by one?

my_list = []

for filename in [file1,file2,file3]:
    with open(filename) as f:
        my_list += [int(i) for line in f for i in line.split()]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • This works it just doesn't output the correct answer. I tested it with just four files and it gave a slightly different answer than what it should have been. Any ideas why this is? – Sekou Jun 18 '17 at 23:24
  • Please define the correct answer by editing your question and add an example. We are not mind readers you know ;) – Jean-François Fabre Jun 19 '17 at 05:08