python多久刷新一次文件? [英] How often does python flush to a file?
本文介绍了python多久刷新一次文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- Python 多久刷新一次文件?
- Python 多久刷新一次标准输出?
我不确定 (1).
至于 (2),我相信 Python 在每个新行之后都会刷新到 stdout.但是,如果您将 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#open
可选的缓冲参数指定文件所需的缓冲区大小:"
"The optional buffering argument specifies the file’s desired buffer size:"
- 0 表示无缓冲,
- 1 表示行缓冲,
- 任何其他正值意味着使用(大约)该大小的缓冲区.
- 负缓冲意味着使用系统默认值,通常为 tty 设备行缓冲,为其他文件完全缓冲.
- 如果省略,则使用系统默认值.
代码:
bufsize = 0
f = open('file.txt', 'w', buffering=bufsize)
这篇关于python多久刷新一次文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文