python合并两个txt文件内容-yolo格式数据
代码
import os
def concattxt(path1,path2):
num = 0
pathdir1 = os.listdir(path1)#获取第一部分txt文件夹中的文件列表
for txtname in pathdir1:
#name1 = os.path.splitext(txtname)[0]#获取当前txt文件名字
#txtfile1 = open(txtpath1+name1, "rb")
#txtfile2 = open(txtpath2+name1, "rb")
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:
#txt1.write('\n')
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中的数据