2017-10-20 227 views
0

好的,我的課程任務是編寫代碼,將每行從一個文本文件複製到新文本文件。我覺得我的頭靠在牆上太過分了,只是看不到我在看什麼。從python中將文件從一個文件複製到另一個文件

這是我有:

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() 
+0

'destination_file.writelines(線)'=>'destination_file.write(線)' –

+1

或'destination_file.writelines(SOURCE_FILE)'無環 –

+0

'numbers_file'是什麼?你永遠不會定義它,並且你不使用據稱從它讀取的值。 – chepner

回答

1
# opens original file 
file1 = open("data1.txt" , "r") 
# opens new file 
file2 = open("data2.txt" , "w") 
#for each line in old file 
for line in file1: 
#write that line to the new file 
    file2.write(line) 
#close file 1 
file1.close() 
#close file2 
file2.clsoe() 
相關問題