combine multiple text files into one text file using python [duplicate]
Asked Answered
B

5

39

suppose we have many text files as follows:

file1:

abc
def
ghi

file2:

ABC
DEF
GHI

file3:

adfafa

file4:

ewrtwe
rewrt
wer
wrwe

How can we make one text file like below:

result:

abc
def
ghi
ABC
DEF
GHI
adfafa
ewrtwe
rewrt
wer
wrwe

Related code may be:

import csv
import glob
files = glob.glob('*.txt')
for file in files:
with open('result.txt', 'w') as result:
result.write(str(file)+'\n')

After this? Any help?

Betts answered 19/7, 2013 at 14:46 Comment(2)
this thread is a ducplicate of #13613836Sext
cat *.txt > all.txtFerdelance
D
89

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())
Disruption answered 19/7, 2013 at 14:59 Comment(6)
thank you. i accepted your answer because your code is powerful. since i am new, can you explain infile.read() procedure.@DisruptionBetts
The .read() method of a file handle reads the contents of a file and produces a string of it's contents.Disruption
nice one apiguy!!Meghan
Is there a way to do it column wise?Valais
The most effective way I came across. Thank you :)Moria
import glob; outfile = open("out.txt", "wb"); [outfile.write(open(f, "rb").read()) for f in glob.glob("*.txt")] just for the fun of having a one-liner :)Geest
F
21

The fileinput module is designed perfectly for this use case.

import fileinput
import glob

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

with open('result.txt', 'w') as file:
    input_lines = fileinput.input(file_list)
    file.writelines(input_lines)
Finnish answered 19/7, 2013 at 15:1 Comment(2)
That's probably the most efficient method and works perfectly in Python 2.7Kerrin
The risk with this code is that if you already have a result.txt file (say, you're running it a second time), the process never ends and builds an ever-increasing sized file.Bummer
M
9

You could try something like this:

import glob
files = glob.glob( '*.txt' )

with open( 'result.txt', 'w' ) as result:
    for file_ in files:
        for line in open( file_, 'r' ):
            result.write( line )

Should be straight forward to read.

Malinin answered 19/7, 2013 at 14:58 Comment(0)
N
1

It is also possible to combine files by incorporating OS commands. Example:

import os
import subprocess
subprocess.call("cat *.csv > /path/outputs.csv")
Noneffective answered 17/5, 2016 at 10:46 Comment(2)
This is not portable approach.Brachium
This is the answer that worked for me! Thanks.Vig
B
0
filenames = ['resultsone.txt', 'resultstwo.txt']
with open('resultsthree', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line)
Brotherson answered 4/6, 2015 at 16:20 Comment(2)
add some explanation to your answer.Decolorant
line by line reading/writing a file is not the same as reading / writing the bytes of a file. just FYI.Mcgrew

© 2022 - 2024 — McMap. All rights reserved.