运行此代码会按预期创建 file2.txt,但该文件为空。(注意:file1.txt 中只有一首诗。)为什么会这样?如何让它将数组 a2 写入文本文件?
import copy
#Open input file, read it into an array, and remove the every other line.
f = open('file1.txt','r')
a1 = f.readlines()
a2 = copy.deepcopy(a1)
f.close
for n in range(len(a1)):
if n%2 == 0:
a2.remove(a1[n])
# Open output file and write array into it.
fo = open('file2.txt','w')
fo.writelines(a2)
fo.close