Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple files into a single stream in Python

Tags:

python

file

mmap

I have a dozen of files that I would like to present to the user as a single read only file like object. I do not want to load them into memory at once, nor merge them in the filesystem. I would like something like a itertools.chain of mmap.mmap but present the API of a file like object (i.e. with the file methods like read, etc). Is this possible?

like image 348
Hernan Avatar asked Nov 10 '22 18:11

Hernan


1 Answers

You can use fileinput module here to read through multiple files.

Lets say for example you want to read two files new.txt and IQ.txt.

for line in fileinput.input(["C:\\Users\\Administrator\\Desktop\\new.txt","C:\\Users\\Administrator\\Desktop\\IQ.txt"]):
print line,

In nutshell you provide a list of files you want to read and do it.

like image 119
vks Avatar answered Nov 14 '22 21:11

vks