.write 在 Python 中不起作用 [英] .write not working in Python
问题描述
我对 Python 还很陌生,所以希望我只是在这里遗漏了一些明显的东西,但这让我很难过.下面是我的程序的片段:
I'm fairly new to Python so hopefully I'm just missing something obvious here, but it has me stumped. Snippet of my program below:
outFile = open('P4Output.txt', 'w')
outFile.write(output)
print output
print "Output saved to "P4Output.txt"
"
输出正确打印到控制台,但如果我打开文件,它是空白的.如果我删除该文件并再次执行我的程序,该文件已创建但仍然是空的.我之前在我的另一个程序中使用了这个完全相同的代码块,它有效,并且仍然有效.但是,如果我打开 Python 并尝试一些简单的操作,例如:
output prints correctly to the console, but if I go open up the file it's blank. If I delete the file and execute my program again, the file is created but still is empty. I used this exact same block of code in another program of mine previously and it worked, and still works. However, if I open up Python and try something simple like:
f = open('test.txt', 'w')
f.write("test")
同样,test.txt 已创建,但保留为空白.什么给?
Again, test.txt is created but is left blank. What gives?
推荐答案
你在程序结束时做了 f.close()
吗?
Did you do f.close()
at the end of your program?
这篇关于.write 在 Python 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!