Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

linux - Concatenate .txt files with same names in different folders with python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The question does not specify what is desired to happen when a file is found in one of the two folders only. Below I assume we want all files, even if they are present in only one folder.

Anyway, taking my best guess at what is desired (happy to change if more details are forthcoming):

def ls_txt_files(path, ext='.txt'):
    """return a list of files under path as a set"""
    return {
        ent.name
        for ent in os.scandir(path)
        if ent.is_file() and ent.name.endswith(ext)
    }

def concat_files(dir1, dir2, outdir, ext='.txt'):
    assert len({dir1, dir2, outdir}) == 3, "all dirs must be different"
    os.makedirs(outdir, exist_ok=True)
    a = ls_txt_files(dir1, ext)
    b = ls_txt_files(dir2, ext)
    for name in a.union(b):
        out = os.path.join(outdir, name)
        with open(out, 'w') as fdst:
            if name in a:
                with open(os.path.join(dir1, name), 'r') as fsrc:
                    shutil.copyfileobj(fsrc, fdst)
            if name in b:
                with open(os.path.join(dir2, name), 'r') as fsrc:
                    shutil.copyfileobj(fsrc, fdst)

(Updated to reflect the comments by @ShadowRanger).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...