多个txt文件内容合并为一个txt文件

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  8 13:08:46 2021

@author: haijiao
用于把多个txt文件合并为一个txt文件
filt_path填所要合并txt文件的目录
path_merges填输出文件目录
out_file_path最后为合并后txt的名字
"""


import os

file_path = "./"
path_merges = "./results_merges"
out_file_path = os.path.join(path_merges, 'train.txt')
file_list = os.listdir(file_path)
txt_list = []
data_list = []

if not os.path.exists(path_merges):
    os.mkdir(path_merges)
if os.path.exists(out_file_path):
    os.remove(out_file_path)

for file in file_list:
    if file.endswith(".txt"):
        txt_list.append(file)
print(txt_list)

for txt_file in txt_list:
    with open(os.path.join(file_path, txt_file), 'r') as f:
        data = f.read()
        with open(out_file_path,"a") as f1:
            f1.write(data)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值