我通常不会在作业上寻求帮助,但我被一个部分问题绊倒了,我似乎不能得到正确的答案。问题是:
Write a function fcopy() that takes as input two file names (as strings) and copies the content of the first file into the second.
到目前为止,我终于想到了:
def fcopy(file1, file2):
os.chdir('C:/Users/HOME/Desktop/Introduction to Computing/Lab/assignments')
open_file_1 = open(file1, 'r')
open_file_2 = open(file2, 'w')
for line in open_file_1:
open_file_2.write(line)
open_file_1.close()
open_file_2.close()
open_file_2 = open(file2, 'r')
for line in open_file_2:
print(line)
open_file_2.close()
我通过查阅许多资料,取得了很大的进展,我想知道如何使这一点发挥作用。谢谢
这应该会有帮助
这是另一种方法
这种方法更好,因为它可以保证outfile将被关闭。你知道吗
从infile和outfile打印内容
第二条路
我相信有更优化的方式做这件事,但这是一个我现在可以做的。如果我找到更好的方法,我会更新我的答案。你知道吗
相关问题 更多 >
编程相关推荐