2016-12-02 48 views
-4

所以我需要做一個代碼,打開一個txt文件,然後把該文件的內容並將其放入另一個txt文件,問題是,我不知道如何從文件中提取信息的命令,我做了一些研究,發現這是最接近的事情,但它只是不是我所需要的:How do I get python to read only every other line from a file that contains a poem如何讓Python從文本文件中讀取和提取單詞?

這是到目前爲止我的代碼:

myFile = open("Input.txt","wt") 
myFile.close() 
myFile = open("Output.txt","wt") 
myFile.close() 
+0

你可能已經錯過了「[讀取和寫入文件(https://docs.python.org/3/tutorial/inputoutput .html#reading-and-writing-files)「。 – Matthias

回答

2

的樣本代碼從一個文件複製文本另一個。也許它會幫助你:

inputFile = open("Input.txt","r") 
text = inputFile.read() 
inputFile.close() 
outputFile = open("Output.txt","w") 
outputFile.write(text) 
outputFile.close() 
0

簡單的只是試試這個

#open input file and read all lines and save it in a list 
fin = open("Input.txt","r") 
f = fin.readlines() 
fin.close() 

#open output file and write all lines in it 
fout = open("Output.txt","wt") 
for i in f: 
    fout.write(i) 
fout.close() 
相關問題