【发布时间】:2021-08-03 02:21:43
【问题描述】:
下面是我将几个 txt 文件合并在一起的代码。
import os
with open('outfile', 'w') as outfile:
for filename in os.listdir(os.getcwd()):
if (filename.endswith(".txt")):
with open(filename) as infile:
for line in infile:
outfile.write(line)
输出文件 'outfile' 是合并后的文件,但它不是 txt。
然后我尝试了
with open('outfile.txt', 'w') as outfile:
进程卡住了,没有任何内容写入 outfile.txt
有谁知道如何修理它?我的目标是将输出文件保存为 txt。
【问题讨论】: