Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I save a text file in python without closing it?

I am writing a program in which I would like to be able to view a log file before the program is complete. I have noticed that, in python (2.7 and 3), that file.write() does not save the file, file.close() does. I don't want to create a million little log files with unique names but I would like to be able to view the updated log file before the program is finished. How can I do this?

Now, to be clear I am scripting using Ansys Workbench (trying to batch some CFX runs). Here's a link to a tutorial that shows what I'm talking about. They appear to have wrapped python, and by running the script I can send commands to the various modules. When the script is running there is no console onscreen and it appears to be eating all of the print statements, so the only way I can report what's happening is via a file. Also, I don't want to bring a console window up because eventually I will just run the program in batch mode (no interface). But the simulations take a long time to run and I can't wait for the program to finish before checking on what's happening.

like image 921
NauticalMile Avatar asked Nov 03 '13 18:11

NauticalMile


People also ask

Do you need to close a text file in Python?

Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Do you need to close file when using with open Python?

However, once Python exits from the “with” block, the file is automatically closed. Trying to read from f after we have exited from the “with” block will result in the same ValueError exception that we saw above. Thus, by using “with”, you avoid the need to explicitly close files.

Can you save a text file in Python?

Saving a Text File in PythonOpening a new file in write mode will create a file and after closing the file, the files get saved automatically. However, we can also write some text to the file. Python provides two methods for the same. write(): Inserts the string str1 in a single line in the text file.

How do you close and save a text file in Python?

To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.


3 Answers

You would need this:

file.flush()
# typically the above line would do. however this is used to ensure that the file is written
os.fsync(file.fileno())

Check this: http://docs.python.org/2/library/stdtypes.html#file.flush

file.flush() Flush the internal buffer, like stdio‘s fflush(). This may be a no-op on some file-like objects. Note flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.

EDITED: See this question for detailed explanations: what exactly the python's file.flush() is doing?

like image 139
starrify Avatar answered Sep 29 '22 20:09

starrify


Does file.flush() after each write help?

Hannu

like image 21
Hannu Avatar answered Sep 29 '22 19:09

Hannu


This will write the file to disk immediately:

file.flush()
os.fsync(file.fileno())

According to the documentation https://docs.python.org/2/library/os.html#os.fsync

Force write of file with filedescriptor fd to disk. On Unix, this calls the native fsync() function; on Windows, the MS _commit() function.

If you’re starting with a Python file object f, first do f.flush(), and then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.

like image 34
Bert Wijnants Avatar answered Sep 29 '22 19:09

Bert Wijnants