![](/img/trans.png)
[英]Is it possible to open PDF files one after other, their names are saved in a text file using Python?
[英]Combination of Content of Multiple Text files into One Text File line after the other using Python
我使用以下代码将多个文本文件合并为一个,
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())
我从上面的代码收到的 output 是
2020-06-22,09:37:39,YZ-5534,72kmph,100kmph,No2020-06-22,09:38:21,CAB-3365,79kmph,100kmph,No2020-06-22,09:40: 42,ABC-5432,98kmph,100kmph,No2020-06-22,09:39:41,CB-3226,117kmph,100kmph,Yes2020-06-22,09:36:54,QX-9904,81kmph,100kmph,不
如何在每个文本文件的内容末尾引入换行符
2020-06-22,09:37:39,YZ-5534,72kmph,100kmph,无
2020-06-22,09:38:21,CAB-3365,79kmph,100kmph,无
2020-06-22,09:40:42,ABC-5432,98kmph,100kmph,无
2020-06-22,09:39:41,CB-3226,117kmph,100kmph,是的
2020-06-22,09:36:54,QX-9904,81kmph,100kmph,无
为了在连接的文本文件中获得上述结果作为最终结果
我尝试了上面的以下代码行但根本不起作用
outfile.write(infile.read()+ '\n')
由于您正在阅读文本文件,您可以试一试吗?
import glob
read_files = glob.glob("*.txt")
with open("result.txt", "w") as outfile:
for f in read_files:
with open(f, "r") as infile:
for line in infile:
outfile.write(line)
outfile.write("\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.