【发布时间】:2017-10-20 17:18:46
【问题描述】:
好的,我的课堂作业是编写代码,将每一行从一个文本文件复制到一个新的文本文件。我觉得我把头撞在墙上太多了,我再也看不到我在看什么了。
这是我所拥有的:
source_file = open('data1.txt', 'r')
line = numbers_file.readline()
destination_file = open('data2.txt', 'w')
source_file.seek(0)
for line in source_file:
destination_file.writelines(line)
source_file.close()
destination_file.close()
【问题讨论】:
-
destination_file.writelines(line)=>destination_file.write(line) -
或
destination_file.writelines(source_file)没有循环 -
什么是
numbers_file?您从不定义它,也没有使用据称从中读取的值。 -
您应该使用
with语句而不是手动关闭文件。