I am running the following code very often to write to files from Python using the following:
def function():
file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../my_file')
fifo = open(file, 'w')
if (os.path.isfile(file) and os.access(file, os.W_OK)):
fifo.write("stuff")
fifo.close()
else:
time.sleep(1)
function()
Unfortunately, I get the following error (not all the time):
IOError: [Errno 2] No such file or directory
This is self-explanatory, but I do not understand then why all the precautions do not work, like isfile
or access..W_OK
? How do I avoid having this issue?
Also, the slower the machine, the less often the error is encountered.