2014-02-22 49 views
0

我試圖讓我的python文件將數字保存到文本文件中,但是當我嘗試它時它總是空白。我以前做過這麼多次,但這次拒絕工作。爲什麼我的文件在python中顯示空白?

openfile = 'example' 
total = 0.5 #another example 
totalstr = str(total) 
file = open("%s.txt" % (openfile), "w") 
file.write(totalstr) 
file.close 
+0

爲什麼文件行縮進? – BitNinja

+2

它是'.close()',你錯過了'()';) – zhangxaochen

+0

在'openfile = file'中將文件作爲字符串'file' – ganesshkumar

回答

2

「文件」 是一個標準的Python類型。你想重新命名一些東西。我也假設「openfile」應該是你想要使用的字符串文件名。到目前爲止的答案都是正確的,但將它們放在一起給出:

my_file_name = "myfile" 
total = 0.5 
my_file_handle = open("%s.txt" %(my_file_name), "w") 
my_file_handle.write(str(total)) 
my_file_handle.close() 
0

這個工作對我來說:

openfile = "file" 
total = 0.5 
totalstr = str(total) 
file = open("%s.txt" % (openfile), "w") 
file.write(totalstr) 
file.close() 

看看你是否能發現變化。

1

file是python中的關鍵字。所以,

print '%s' %(file) 

打印

<type 'file'> 

你應該使用:

openfile = 'file' 
相關問題