我是靠谱客的博主 老迟到长颈鹿,这篇文章主要介绍多个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)

最后

以上就是老迟到长颈鹿最近收集整理的关于多个txt文件内容合并为一个txt文件的全部内容,更多相关多个txt文件内容合并为一个txt文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部