合并多个text文件内容
想要将某文件夹下面的所有.txt文件内容放到一个.txt中,使用txt的写入写出就可以了,代码如下:
import os
import sys
# 获取当前路径
jupyter_path = os.getcwd()
# 获取txt文件夹路径
in_path = os.path.join(jupyter_path, "txt")
out_path = jupyter_path
with open(os.path.join(jupyter_path, "all.txt"), "w") as f:
# 获取txt文件夹下的所有文件
for file in os.listdir(in_path):
with open(os.path.join(in_path, file), "r") as tmp_f:
for line in tmp_f.readlines():
f.write(line)