.write在Python中不起作用 [英] .write not working in Python

查看:142
本文介绍了.write在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\"\n"

输出可正确打印到控制台,但是如果我打开文件,则该文件为空白.如果删除文件并再次执行程序,则文件已创建,但仍然为空.我以前在我的另一个程序中使用了完全相同的代码块,并且该代码块仍然有效.但是,如果我打开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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆