我想用Python把几个txt文件合并成一个文件。我已经找到了这样的代码:
import glob
read_files = glob.glob("*.txt")
with open("result.txt", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(infile.read())但我想保留单个txt文件的名称,并在合并后的文件中将它们打印在每个txt文件的上方。你知道怎么解决这个问题吗?
发布于 2021-04-29 14:54:24
这应该是可行的
import glob
read_files = glob.glob("*.txt")
with open("result.txt", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(bytes(f"{f}\n", 'utf-8'))
outfile.write(infile.read())https://stackoverflow.com/questions/67312248
复制相似问题