Chinaunix
标题:
python写入文件的奇怪问题
[打印本页]
作者:
jeffinier
时间:
2016-06-29 18:14
标题:
python写入文件的奇怪问题
就是下面这样写法,无法打印出任何结果,结果发现在程序运行过程中 /tmp/test一直为空,直到运行结束才会被写入,导致grep无法获取结果,有什么办法吗? ubuntu的系统
import commands
import time
r = "abcd"
f=file("/tmp/test", 'w')
f.write(r)
(status, result) = commands.getstatusoutput("grep ab /tmp/test")
time.sleep(5)
print result
作者:
Linux_manne
时间:
2016-06-29 18:17
关闭文件对象
作者:
reyleon
时间:
2016-06-30 09:23
import commands
import time
r = "abcd"
f=file("/tmp/test", 'w')
f.write(r)
f.close()
(status, result) = commands.getstatusoutput("grep ab /tmp/test")
time.sleep(5)
print result
作者:
icymirror
时间:
2016-06-30 11:41
回复
1#
jeffinier
在
f.write(r)
复制代码
之后加上:
f.flush()
复制代码
来确保文件被写入磁盘
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2