I have two txt files like this: txt1:
Foo
Foo
Foo
Foo
txt2:
Bar
Bar
Bar
Bar
How can I concatenate them in a new file by the left and the right side let's say like this:
Bar Foo
Bar Foo
Bar Foo
Bar Foo
I tried the following:
folder = ['/Users/user/Desktop/merge1.txt', '/Users/user/Desktop/merge2.txt']
with open('/Users/user/Desktop/merged.txt', 'w') as outfile:
for file in folder:
with open(file) as newfile:
for line in newfile:
outfile.write(line)
Use itertools.izip
to combine the lines from both the files, like this
from itertools import izip
with open('res.txt', 'w') as res, open('in1.txt') as f1, open('in2.txt') as f2:
for line1, line2 in izip(f1, f2):
res.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
Note: This solution will write lines from both the files only until either of the files exhaust. For example, if the second file contains 1000 lines and the first one has only 2 lines, then only two lines from each file are copied to the result. In case you want lines from the longest file even after the shortest file exhausts, you can use itertools.izip_longest
, like this
from itertools import izip_longest
with open('res.txt', 'w') as res, open('in1.txt') as f1, open('in2.txt') as f2:
for line1, line2 in izip_longest(f1, f2, fillvalue=""):
res.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
In this case, even after the smaller file exhausts, the lines from the longer file will still be copied and the fillvalue
will be used for the lines from the shorter file.
You can use zip
to zip those lines then concatenate and write them in your outfile:
folder = ['/Users/user/Desktop/merge1.txt', '/Users/user/Desktop/merge2.txt']
with open('/Users/user/Desktop/merged.txt', 'w') as outfile:
for file in folder:
with open(file[0]) as newfile,open(file[1]) as newfile1:
lines=zip(newfile,newfile1)
for line in lines:
outfile.write(line[0].rstrip() + " " + line[1])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With