As per the docs
first do
f.flush(), and then doos.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.
Here is an example,
import os
with open("filename", "w") as f:
while True: # infinite program
f.write("the text")
f.flush()
os.fsync(f.fileno())
Answer from Shadow on Stack OverflowAs per the docs
first do
f.flush(), and then doos.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.
Here is an example,
import os
with open("filename", "w") as f:
while True: # infinite program
f.write("the text")
f.flush()
os.fsync(f.fileno())
1) Get the PID of your python process
pgrep python
2) List file descriptors
ls -l /proc/{PID}/fd
3) Open gdb
$ gdb
(gdb) attach {PID}
(gdb) call fflush({FILE_DESCRIPTOR})
(gdb) detach
4) Check your file
Writing to File Using Buffer In Python - Stack Overflow
Buffer protocol types
How write image buffer to file in python game engine bge 2.62 ?
Python writes to a buffer instead of a file - Stack Overflow
Looks like it writes the output to some buffer
That's correct. You can disable this with flush=True, so:
while True:
print('tick', flush=True)
time.sleep(5)
Try this:
with open('log.log', 'w+') as log_file:
print('tick', file=log_file)
In that way the print output will go to file instead stdout
An example usage:
>>> s = 'Hello world'
>>> t = buffer(s, 6, 5)
>>> t
<read-only buffer for 0x10064a4b0, size 5, offset 6 at 0x100634ab0>
>>> print t
world
The buffer in this case is a sub-string, starting at position 6 with length 5, and it doesn't take extra storage space - it references a slice of the string.
This isn't very useful for short strings like this, but it can be necessary when using large amounts of data. This example uses a mutable bytearray:
>>> s = bytearray(1000000) # a million zeroed bytes
>>> t = buffer(s, 1) # slice cuts off the first byte
>>> s[1] = 5 # set the second element in s
>>> t[0] # which is now also the first element in t!
'\x05'
This can be very helpful if you want to have more than one view on the data and don't want to (or can't) hold multiple copies in memory.
Note that buffer has been replaced by the better named memoryview in Python 3, though you can use either in Python 2.7.
Note also that you can't implement a buffer interface for your own objects without delving into the C API, i.e. you can't do it in pure Python.
I think buffers are e.g. useful when interfacing Python to native libraries (Guido van Rossum explains buffer in this mailing list post).
For example, NumPy seems to use buffer for efficient data storage:
import numpy
a = numpy.ndarray(1000000)
The a.data is a:
<read-write buffer for 0x1d7b410, size 8000000, offset 0 at 0x1e353b0>