python合并两个txt文件内容-yolo格式数据
代码
import os
def concattxt(path1,path2):
num = 0
pathdir1 = os.listdir(path1)
for txtname in pathdir1:
txtfilepath1 = path1+txtname
txtfilepath2 = path2+txtname
txt1 = open(txtfilepath1,'a+', encoding='utf-8')
if os.path.exists(txtfilepath2):
num = num+1
with open(txtfilepath2, 'r', encoding='utf-8') as txt2:
for i in txt2:
txt1.write(i)
print('the concat txt num is:',num)
if __name__ == '__main__':
txtpath1 = '/home/jun/work/2021Z001/test/ann/'
txtpath2 = '/home/jun/work/2021Z001/test/labels/'
concattxt(txtpath1,txtpath2)
效果
txt1中的原始数据
txt2中的原始数据 合并后txt1中的数据
|