hoju
hoju

Reputation: 29452

write to status file fast

My Python script needs to write status to a text file several times a second, overriding the previous content.

fp = open('status.txt', 'w')
fp.write('12983214/23180100')
fp.close()

How would you recommend implementing this to minimize time spent here? Command line call? Separate thread?

Upvotes: 1

Views: 1387

Answers (2)

liupeixin
liupeixin

Reputation: 738

"write status to a text file several times a second"
I think you can save the file instance until wouldn't to write to it. Now to close file instance. Before close you can write many times.
open and close usually is costs some efficiency.

"overriding the previous content"
if you override the previous content why not write a final result in the end time?

Upvotes: 1

agf
agf

Reputation: 176780

It looks like you're writing out a progress number. That means your string will never get shorter, only longer.

Because of that, you don't have to truncate it before each write -- just seek back to the beginning and write over the previous value. Follow that up with a flush so that the data will be available to whatever is monitoring the status.

Doing this several times a second shouldn't take any significant amount of time, and it's the simplest solution.

fp = open('status.txt', 'w')

#whenever you want to write
fp.seek(0)
fp.write('12983214/23180100')
fp.flush()

#when you're done
fp.close()

If the status string can get shorter, just add

fp.truncate(0)

before the write. You still need to seek as well because truncating doesn't change the current file position.

All of these methods are documented under built-in file objects.

Upvotes: 7

Related Questions