Reputation: 925
I have a python code which looks something like this:
os.system("...../abc.bat")
open("test.txt")
.....
Where the abc.bat upon complete run creates test.txt. Now the problem is that since there is no "wait" here code directly goes to open("file.txt") and naturally does not find it and hence the problem.
Is there any way I can find out status if the bat run has finished and now the python can move to open("test.txt")?
Note: I have used an idea where in the bat file has command- type nul>run.ind and this file is deleted by the bat file at the end as an indicator that bat run has finished. Can I somehow make use of this in the python code like :
os.system("...../abc.bat")
if file run.ind exists == false:
open ("test.txt")
This seems to be somewhat crude. Are there any other methods to do this in simpler way? or in other words is it possible to introduce some "wait" till the bat run is finished?
Upvotes: 0
Views: 1670
Reputation: 1390
Answer to an old question but it could prove helpful for new people ending up here
I've implemented a python package that waits for things
https://pypi.org/project/wait-for-it-to/
install like this:
pip install wait-for-it-to
and use like this in your example
import os
import wait_For_it_to
os.system("...../abc.bat")
wait_for_it_to.be_true(os.path.exists, timeout=60,args=['test.txt'])
Upvotes: 0
Reputation: 1091
You could create a loop and try to rename the text file (to itself) within the loop. This will only succeed when the text file has completed.
Upvotes: 0
Reputation: 465
Maybe you could try to put it on a while loop.
import os
import time
os.system("...../abc.bat")
while True:
try:
o = open("test.txt", "r")
break
except:
print "waiting the bat.."
time.sleep(15) # Modify (seconds) to se sufficient with your bat to be ready
continue
Upvotes: 1
Reputation: 180917
I'm unsure of the wait semantics of os.system
on all platforms, but you could just use subprocess.call() which the documentation for os.system
is pointing to;
Run the command described by args. Wait for command to complete, then return the returncode attribute.
Upvotes: 4