I have two folders containing many text files with matching file names. So I am concatenating folder1/file1.txt with folder2.file1.txt. My current code appends data from folder2/file1 to folder2/file1 instead from two different folders.
import glob,os
path='/my/dir'
data = data2 = ""
#read files with only.txt extension
for filename in glob.glob('.txt'):
#open folder1/file1 for reading
with open(path, filename,'r') as fp:
data=fp.read()
print(filename)
#open folder2/file1 for reading
with open(os.path.join(path, "/output", filename, 'r')) as fp:
data2=fp.read()
data +="
"
data +=data2
#open output file for writing
with open (filename,"-new",'.txt', w) as fp:
fp.write(data)
For python 2.7 I modified the code like below
import glob,os,shutil
from os import listdir
from os.path import isfile, join
path='/My/dir'
outdir='/My/dir/merged'
fol1='/my/dir/input1'
fol2='/my/dir/input2'
def concat_files(dir1, dir2, outdir, ext='.txt'):
assert len({dir1, dir2, outdir}) == 3, "all dirs must be different"
os.makedirs(outdir)
a = os.listdir(dir1)
b = os.listdir(dir2)
for item1 in a:
for item2 in b:
if(item1==item2):
out = os.path.join(outdir, item1)
with open(out, 'w') as fdst:
if item1 in a:
with open(os.path.join(dir1, item1), 'r') as fsrc:
shutil.copyfileobj(fsrc, fdst)
if item2 in b:
with open(os.path.join(dir2, item2), 'r') as fsrc:
shutil.copyfileobj(fsrc, fdst)
print("Your merged file is in " ,outdir)
concat_files(fol1, fol2, outdir)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…