2015-04-14 66 views
-3

我需要編寫一個程序,將數字添加到一個簡單的.txt文件。 我該怎麼處理?我需要使用輸入功能。如何將數字添加到file.txt?

難道我去,如:

file = open("file.txt") 
numbers = input("Enter a number: ") 
numbers = file.w() 
print (numbers) 
file.close() 

我不知道,因爲這是我的一個新的課題。

回答

0

這個例子

#!/usr/bin/python 

# Open a file 
fo = open("foo.txt", "wb") 
fo.write("Python is a great language.\nYeah its great!!\n"); 

# Close opend file 
fo.close() 

從這裏拍攝:http://www.tutorialspoint.com/python/python_files_io.htm

你最快的方法是把numbers變量fo.write()作爲參數:

fo.write(numbers)