![](/img/trans.png)
[英]How to concatenate many txt files with different names into one array with numpy on python?
[英]Concatenate .txt files with same names in different folders with python
我有兩個文件夾,其中包含許多具有匹配文件名的文本文件。 所以我將folder1/file1.txt 與folder2.file1.txt 連接起來。 我當前的代碼將數據從 folder2/file1 附加到 folder2/file1 而不是來自兩個不同的文件夾。
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 +="\n"
data +=data2
#open output file for writing
with open (filename,"-new",'.txt', w) as fp:
fp.write(data)
對於 python 2.7 我修改了如下代碼
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)
該問題未指定僅在兩個文件夾之一中找到文件時希望發生的情況。 下面我假設我們想要所有文件,即使它們只存在於一個文件夾中。
無論如何,我最好的猜測是什么(如果有更多細節,很高興改變):
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)
(更新以反映@ShadowRanger 的評論)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.