How to Concatenate Text Files in Python

How do I concatenate text files in Python?

This should do it

For large files:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)

For small files:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())

… and another interesting one that I thought of:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for line in itertools.chain.from_iterable(itertools.imap(open, filnames)):
outfile.write(line)

Sadly, this last method leaves a few open file descriptors, which the GC should take care of anyway. I just thought it was interesting

How to concatenate a large number of text files in python

Use glob.glob to build list files and shutil.copyfileobj for a more efficient copy.

import shutil
import glob

filenames = glob.glob("*.txt") # or "file*.txt"
with open("output_file.txt", "wb") as outfile:
for filename in filenames:
with open(filename, "rb") as infile:
shutil.copyfileobj(infile, outfile)

combine multiple text files into one text file using python

You can read the content of each file directly into the write method of the output file handle like this:

import glob

read_files = glob.glob("*.txt")

with open("result.txt", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(infile.read())

Python: concatenating text files

Maybe not the most elegant but...

length = 10
txt = [f"text_{n}.txt" for n in range(length)]
com = [f"comments_{n}.txt" for n in range(length)]
feed = [f"feedback_{n}.txt" for n in range(length)]

for f, t, c in zip(feed, txt, com):
with open(f, "w") as outfile:
with open(t) as infile1:
contents = infile1.read()
outfile.write(contents)
with open(c) as infile2:
contents = infile2.read()
outfile.write(contents)

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())

It looks like you just want to write a file in the end. So why not?

It looks like you just want to write a file in the end. So why not?:

def cat(f1, f2):
with open(f1, 'r') as f:
f1txt = f.read()
with open(f2, 'r') as f:
f2txt = f.read()

return f1txt + f2txt

def my_fun(f3, text):
with open(f3, 'w') as f:
f.write(text)

out = '/path/to/some/file'
file1 = '/path/to/file1'
file2 = '/path/to/file2'

my_fun(cat(file1, file2))

This will read all the data inside file1, then file2 and then add all the data from file2 to the end of the file1 data. If you mean to concatenate another way, please specify.

Concatenation of Text Files (.txt files) with list in python?

You could try something like this:

inputText = []
for file in ['1.txt','2.txt']:
with open(file,'r') as file:
inputText.extend(file.readlines()+['\n'])
with open ('3.txt','w') as output:
for line in inputText:
output.write(line)

or

with open ('3.txt','w') as output:
for file in ['1.txt','2.txt']:
with open(file,'r') as file:
for line in file:
output.write(line)
output.write('\n')

Edited to your comment:

import re
inputList = []
for file in ['1.txt','2.txt']:
with open(file,'r') as infile:
for line in infile:
inputList.extend(re.sub('[^A-Za-z0-9,]+', '', line).split(","))
print(inputList)
with open('3.txt','w') as outfile:
for line in inputList:
outfile.write(line + '\n')

Concatenating two text files in Python

Of course Python can be used for text processing (possibly it is even better suited for that than for numerical jobs). The task in question, however, can be done with a single Unix command: paste A.txt B.txt > output.txt

And here is a Python solution without using numpy:

 with open('A.txt') as a:
with open('B.txt') as b:
with open('output.txt', 'w') as c:
for line_a, line_b in zip(a, b):
c.write(line_a.rstrip() + ' ' + line_b)


Related Topics



Leave a reply



Submit