Vasiliy Sharapov
Vasiliy Sharapov

Reputation: 1057

Verifying that a file is ready for reading in py3k

I'm writing to a file for another program (gnuplot) to use as input.

g = open('test.gnuplot', 'w')
g.write("[snip]")
g.close()

os.system("sleep 1")
os.system("gnuplot test.gnuplot")

If I omit the sleep 1 gnuplot generates a mangled file - seemingly because the file I just wrote isn't "ready" yet (if I'm wrong please tell me what the real reason is).

I'm guessing there is a more elegant way to wait for the file to be ready for reading, what is it?

Upvotes: 1

Views: 269

Answers (1)

Jason Yeo
Jason Yeo

Reputation: 3702

Try this:

g = open('test.gnuplot', 'w')
g.write("[snip]")
g.flush()
os.fsync(g.fileno()) 
os.system("gnuplot test.gnuplot")

And take away the os.system("sleep 1"). I think the buffer is not yet written to the file. See the python fsync docs.

Upvotes: 1

Related Questions