Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there COMMIT analog in python for writing into a file?

I have a file open for writing, and a process running for days -- something is written into the file in relatively random moments. My understanding is -- until I do file.close() -- there is a chance nothing is really saved to disk. Is that true?

What if the system crashes when the main process is not finished yet? Is there a way to do kind of commit once every... say -- 10 minutes (and I call this commit myself -- no need to run timer)? Is file.close() and open(file,'a') the only way, or there are better alternatives?

like image 404
user63503 Avatar asked Mar 03 '09 21:03

user63503


People also ask

How do you write data into a file in Python?

Writing to Files in Python Writing a string or sequence of bytes (for binary files) is done using the write() method. This method returns the number of characters written to the file. This program will create a new file named test.txt in the current directory if it does not exist. If it does exist, it is overwritten.

Which method take a string and write it in the file in Python?

The write() method: This function inserts the string into the text file on a single line. Based on the file we have created above, the below line of code will insert the string into the created text file, which is "myfile. txt.”

How do you create a dynamic file in Python?

If you need to create a file "dynamically" using Python, you can do it with the "x" mode. With this mode, you can create a file and then write to it dynamically using methods that you will learn in just a few moments.


4 Answers

You should be able to use file.flush() to do this.

like image 109
zweiterlinde Avatar answered Nov 15 '22 19:11

zweiterlinde


If you don't want to kill the current process to add f.flush() (it sounds like it's been running for days already?), you should be OK. If you see the file you are writing to getting bigger, you will not lose that data...

From Python docs:

write(str) Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the flush() or close() method is called.

It sounds like Python's buffering system will automatically flush file objects, but it is not guaranteed when that happens.

like image 39
Jason Coon Avatar answered Nov 15 '22 21:11

Jason Coon


To make sure that you're data is written to disk, use file.flush() followed by os.fsync(file.fileno()).

like image 43
Benjamin Peterson Avatar answered Nov 15 '22 20:11

Benjamin Peterson


As has already been stated use the .flush() method to force the write out of the buffer, but avoid using a lot of calls to flush as this can actually slow your writing down (if the application relies on fast writes) as you'll be forcing your filesystem to write changes that are smaller than it's buffer size which can bring you to your knees. :)

like image 23
Christian Witts Avatar answered Nov 15 '22 20:11

Christian Witts