0

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?

1
  • 1
    I would expect that os.system() would wait for execution to finish. Can you tell us what abc.bat is doing? Is it launching something that runs asynchronously?
    – mojo
    Commented Aug 7, 2013 at 13:34

4 Answers 4

4

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.

1

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
0

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.

0

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'])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.