2017-05-14 93 views
0

我想根據變量創建一個名稱爲文件的文件。這是我的第一個問題,然後我想寫在控制檯的屏幕上打印什麼文件。創建並寫入文件 - Python

這就是我所擁有的,我目前在.open得到一個錯誤。 - '掃描字符串文字時出現EOL'。不過我猜想會有更多的錯誤需要遵循。

saveFile.open((fileName)".txt,'w') 
saveFile.write("Final Quote Price: £", format(FinalPrice, '.2f'), "Plus VAT") 
saveFile.close() 

我只是把一個例子在.WRITE一部分,但我希望它寫的是什麼打印到控制檯到文件屏幕的某些部分 - 這可能嗎?

+0

任何人都知道如何定義saveFile? –

回答

0

我想你錯過了你的變量和字符串文字之間的+saveFile.open((fileName)+".txt,'w')

0

有缺失報價:

saveFile = open(fileName+".txt",'w') 
saveFile.write("Final Quote Price: £{:.2f} Plus VAT".format(FinalPrice)) 
saveFile.close() 
+0

謝謝,我現在的問題是saveFile沒有正確定義,我該怎麼做? –

+0

也許你想將'saveFile'定義爲標準'open'函數的返回值。 – Daniel

0
saveFile.open(fileName + ".txt",'w') 
saveFile.write("Final Quote Price: £" + format(FinalPrice, '.2f') + "Plus VAT") 
saveFile.close() 

這應該工作

0

。在你的代碼,失去了一個雙配額,試試這個,

saveFile.open((fileName)+".txt",'w') 
saveFile.write("Final Quote Price: £", format(FinalPrice, '.2f'), "Plus VAT") 
saveFile.close() 
0

要寫並從具有變量名稱的文件中讀取日期:

fileName = 'fileData' 
finalPrice = 12.003321 

fileObj = open(fileName + ".txt", 'w') 
fileObj.write("Final Quote Price: £" + format(finalPrice, '.2f') + "Plus VAT") 
fileObj.close() 

with open(fileName + '.txt', 'r') as file: 
    fileString = file.read() 
    print("Line: " + fileString)