python多长时间刷新一个文件? [英] How often does python flush to a file?

查看:167
本文介绍了python多长时间刷新一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. Python与文件冲洗的频率如何?

  2. Python与stdout的冲突频率如何?

我不确定(1)。

至于(2),我相信Python会在每个新的线。但是,如果您将stdout重载为一个文件,它是否会经常刷新?

As for (2), I believe Python flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?

推荐答案

对于文件操作,Python使用操作系统的默认缓冲,除非您配置它否则。您可以指定缓冲区大小,无缓冲或线缓冲。

For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.

例如,open函数需要一个缓冲区大小参数。

For example, the open function takes a buffer size argument.

http://docs.python.org/library/functions.html#打开

可选缓冲参数指定文件所需的缓冲区大小: 0表示无缓冲,1表示线缓冲,任何其他正值表示使用一个大约为(大约)的缓冲区,一个负缓存意味着使用系统默认,通常是针对tty设备的线路缓冲,并为其他文件完全缓冲,如果省略,则使用系统默认值。 p>

"The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used."

bufsize = 0
f = open('file.txt', 'w', bufsize)

这篇关于python多长时间刷新一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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