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

2024/11/20 22:25:16

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 readingwith open(path, filename,'r') as fp:data=fp.read()print(filename)
#open folder2/file1 for readingwith open(os.path.join(path, "/output", filename, 'r')) as fp:data2=fp.read()data +="\n"data +=data2
#open output file for writingwith 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)
Answer

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.namefor 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).

https://en.xdnf.cn/q/118363.html

Related Q&A

Importing module via another module

In module A, I import module B. Then, in module C, I import module A. In module C, will I be able to use the content of module B implicitly via the import of module A, or will I have to explicitly impo…

Why time.time() gives 0.0?

I have a python program defined by a function myFunc(m,n)Basically, the function contains two for loops.def myFunc(m, n) : for i in range(m) : for j in range(n) : # do it ...return I would like to calc…

Function reads np.array - produces the mean for k nn to number p in np.array

I need to defina a function which reads a numpy array and produces the mean for k nearest points to number p in the array. Example: array= np.array([1, 2, 3, 4, 5, 6, 7, 50, 24, 32, 9, 11, 12, 10]) p= …

How to plot a line over a bar chart

I am trying to plot a line over a bar chart, but when I plotted the line the bar chart disappeared - and so did my x index values. Can anyone help me plot the line and the bar in the same figure? Than…

How to trim spaces between list elements in an f-string? [duplicate]

This question already has answers here:Print all items in a list with a delimiter(8 answers)Closed 2 months ago.I have a string I am formatting and printing lists using f-string and I need to eliminate…

Keeping name and score together while sorting

so I need to sort some high scores into order and here is the code I already have:def sortscores():namelist = []scorelist = []hs = open("hst.txt", "r")hscounter = 0for line in hs:if…

How to put values of pandas dataframe into a for loop in python?

This is a part of a Python API Connection program Here is the DataFrame SampleRegion Sector Brand ID Start Date 7188 US 41 40000 2006-03-06 7189 US 41 40345 2017-11-06 …

Partition Array

Given an array nums of integers and an int k, partition the array (i.e move the elements in nums) such that: All elements < k are moved to the left. All elements >= k are moved to the right Retur…

Tensorflow model accuracy

My model which I have trained on a set of 29K images for 36 classes and validated on 7K images. The model has a training accuracy of 94.59% and validation accuracy of 95.72% It has been created for OCR…

python email.message_from_string() parse problems

My setup uses fetchmail to pull emails from Gmail, which are processed by procmail and passes it to a python script.When I use email.message_from_string(), the resulting object is not parsed as an emai…