Concatenate multiple files into a single file object without creating a new file

Use input from fileinput module. It reads from multiple files but makes it look like the strings are coming from a single file. (Lazy line iteration).

import fileinput

files= ['F:/files/a.txt','F:/files/c.txt','F:/files/c.txt']

allfiles = fileinput.input(files)

for line in allfiles: # this will iterate over lines in all the files
    print(line)

# or read lines like this: allfiles.readline()

If you need all the text in one place use StringIO

import io

files= ['F:/files/a.txt','F:/files/c.txt','F:/files/c.txt']


lines = io.StringIO()   #file like object to store all lines

for file_dir in files:
    with open(file_dir, 'r') as file:
        lines.write(file.read())
        lines.write('\n')

lines.seek(0)        # now you can treat this like a file like object
print(lines.read())

try something along this lines:

def read_files(*filenames):
    for filename in filenames:
        with open(filename,'r') as file_obj:
            for line in file_obj:
                yield line

you can call it with

for line in read_files("f1.txt", "f2.txt", "f3.txt"):
    #... do whatever with the line

or

filenames = ["f1.txt", "f2.txt", "f3.txt"]
for line in read_files(*filenames):
    #... do whatever with the line

You can use fileinput package. This module implements a helper class and functions to quickly write a loop over a list of files

import fileinput
with fileinput.input(files=('file1.txt', 'file2.txt', 'file3.txt')) as f:
    for line in f:
      #rest code

Let's say multiple_files is a list which contain all file names

multiple_files = ["file1.txt", "file2.txt", "file3.txt", ...] # and so on...

Open the output file which will contain all

f = open("multiple_files.txt", "w")
for _file in multiple_files:
    f.write(_file.read())

This way you don't have to read each and every line of your files.

Although the above method is simpler, You also have fileinput module as an alternative.

fileinput docs

You can use fileinput to access and process multiple files.

Example:

with fileinput.input(files=('file1.txt', 'file2.txt')) as f:
    for line in f:
        process(line)

Tags:

Python

File