【发布时间】:2021-11-02 17:48:49
【问题描述】:
我想将忽略代码中的 cmets 的 Python 脚本复制到另一个文件。我已经尝试了下面的代码,但它复制了包括 cmets 在内的所有代码行。请让我知道如何纠正它。我是 Python 编程的新手。
with open("essays.txt",'rb') as f1:
with open("myfile.txt",'wb') as f2:
while True:
buf=f1.readline()
if len(buf)!=0:
if buf[0]=='#':
continue
else:
f2.write(buf)
else:
break
print("File Copied")
以上是将file1的所有内容复制到file2中,包括输出中不需要的cmets。
【问题讨论】:
标签: while-loop file-handling readline with-statement continue