Python 2.7:立即写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18984092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 2.7 : Write to file instantly
提问by elbajo
I realized that when I write into a file using python it wait until the end of my Python file to execute it:
我意识到当我使用 python 写入文件时,它会等到我的 Python 文件结束才执行它:
outputFile = open("./outputFile.txt","a")
outputFile.write("First")
print "Now you have 10sec to see that outputFile.txt is still the same as before"
time.sleep(10)
outputFile.write("Second")
print "Now if you look at outputFile.txt you will see 'First' and 'Second'"
How am I suppose to make python write instantly to the output file?
我该如何让 python 立即写入输出文件?
采纳答案by RyPeck
回答by ismail
Force it with the flush()
function, add
用flush()
函数强制它,添加
outputFile.flush()
at the end of your code.
在您的代码末尾。
回答by ffeast
As @RyPeck said you can use flush()
or set the file object to be
unbuffered.
But note the following (from
https://docs.python.org/2/library/stdtypes.html?highlight=file%20flush#file.flush):
正如@RyPeck 所说,您可以使用flush()
或将文件对象设置为无缓冲。但请注意以下内容(来自
https://docs.python.org/2/library/stdtypes.html?highlight=file%20flush#file.flush):
Flush the internal buffer, like stdio‘s fflush().
Note flush() does not necessarily write the file's data to disk. Use flush() followed by os.fsync() to ensure this behavior.
刷新内部缓冲区,就像 stdio 的 fflush() 一样。
注意flush() 不一定将文件的数据写入磁盘。使用 flush() 后跟 os.fsync() 来确保这种行为。
And a quote from man 3 fflush
:
并引用自man 3 fflush
:
Note that fflush() only flushes the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).
请注意, fflush() 仅刷新 C 库提供的用户空间缓冲区。为了确保数据物理存储在磁盘上,内核缓冲区也必须刷新,例如,使用 sync(2) 或 fsync(2)。
回答by bgoodr
Just to combine all of the above answers into a set of useful utility functions because a key requirement of the OP(and myself!) is "because I don't want to write outputFile.flush() each time":
只是将上述所有答案组合成一组有用的实用程序函数,因为OP(和我自己!)的一个关键要求是“因为我不想每次都编写 outputFile.flush()”:
import os
import tempfile
import time
def write_now(filep, msg):
"""Write msg to the file given by filep, forcing the msg to be written to the filesystem immediately (now).
Without this, if you write to files, and then execute programs
that should read them, the files will not show up in the program
on disk.
"""
filep.write(msg)
filep.flush()
# The above call to flush is not enough to write it to disk *now*;
# according to https://stackoverflow.com/a/41506739/257924 we must
# also call fsync:
os.fsync(filep)
def print_now(filep, msg):
"""Call write_now with msg plus a newline."""
write_now(filep, msg + '\n')
# Example use with the with..as statement:
with tempfile.NamedTemporaryFile(prefix='some_prefix_here.', suffix='.log', dir='.', delete=False) as logf:
print_now(logf, "this is a test1")
time.sleep(20)
print_now(logf, "this is a test2")