【发布时间】:2020-08-13 08:04:41
【问题描述】:
如何检查文本文件中的每一行并删除每行开头的空格?
这是我尝试过的,但它给了我一个空白文件
with open("0.txt","r") as readfile, open("0.txt","w") as outfile:
for line in readfile:
if line.startswith(' '):
outfile.write(line.replace(' ', ''))
【问题讨论】:
-
你正在从同一个文件(
0. text)读写,open创建一个新文件,使用不同的文件名进行写入。