簡體   English   中英

為什么我的文件在python中顯示為空白?

[英]Why does my file show up blank in python?

我正在嘗試獲取python文件以將數字保存到文本文件中,但嘗試時它始終為空白。 我之前已經做過很多次,但是這次拒絕了。

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

“文件”是標准的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()

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

print '%s' %(file)

版畫

<type 'file'>

您應該使用:

openfile = 'file'

這對我有用:

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

查看是否可以發現更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM