One Python process writes status updates to a file for other processes to read. In some circumstances, the status updates happen repeatedly and quickly in a loop. The easiest and fastest approach is to use to open().write() in one line:
open(statusfile,'w').write(status)
An alternate approach with four lines that force the data to disk. This has a significant performance penalty:
f = open(self.statusfile,'w')
f.write(status)
os.fsync(f)
f.close()
I'm not trying to protect from an OS crash. So, does the approach force the data to the OS buffer so other processes read the newest status data when they open the file from disk? Or, do I need to use os.fsync()?
OK, but what happens when you try to open a file for writing using a filename that already exists? Nothing, error-wise. But whatever file that existing filename pointed to is basically wiped out. You may get an error message if you attempt to write to a path that points to a directory or some kind of protected file.
Python provides inbuilt functions for creating, writing, and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s, and 1s).
Definition and Usage The write() method writes a specified text to the file.
The read mode in Python opens an existing file for reading, positioning the pointer at the file's start. Note: If the file does not exist, Python throws an error. In all cases, the function returns a file object and the characteristics depend on the chosen mode.
No, the first approach does not guarantee that the data is written out, since it is not guaranteed that the file will be flushed and closed once the handle is no longer referenced by its write
member. This is likely the case with CPython, but not necessarily with other Python interpreters; it's an implementation detail of the Python garbage collector.
You should really use the second approach, except that os.fsync
is not needed; just close the file and the data should be available to other processes.
Or, even better (Python >=2.5):
with open(self.statusfile, 'w') as f:
f.write(status)
The with
version is exception-safe: the file is closed even if write
fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With