2017-10-20 10 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() 
関連する問題