Search code examples
python-2.7wait

Waiting on another python process to continue


Python version: Python 2.7.13

I am trying to write a script that is able to go through a *.txt file and launch a batch file to execute a particular test.

The code below goes through the input file, changes the string from 'N' to 'Y' which allows the particular test to be executed. I am in the process of creating a for loop to go through all the lines within the *.txt file and execute all the test in a sequence. However, my problem is that I do not want to execute the test at the same time (which is what would happen if I just write the test code).

Is there a way to wait until the initial test is finished to launch the next one?

Here is what I have so far:

from subprocess import Popen
import os, glob

path = r'C:/Users/user1/Desktop/MAT'

for fname in os.listdir(path):
    if fname.startswith("fort"):
        os.remove(os.path.join(path, fname))

with open('RUN_STUDY_CHECKLIST.txt', 'r') as file:
    data = file.readlines()

ln = 4

ch = list(data[ln])
ch[48] = 'Y'
data[ln] = "".join(ch)

with open('RUN_STUDY_CHECKLIST.txt', 'w') as file:
    file.writelines(data)

matexe = Popen('run.bat', cwd=r"C:/Users/user1/Desktop/MAT")
stdout, stderr = matexe.communicate()

In this particular instance I am changing the 'N' in line 2 of the *.txt file to a 'Y' which will be used as an input for another python script.

I have to mention that I would like to do this task without having to interact with any prompt, I would like to do execute the script and leave it running (since it would take a long time to go through all the tests).

Best regards,

Jorge


Solution

  • After further looking through several websites I managed to get a solution to my question.

    I used:

    exe1 = subprocess.Popen(['python', 'script.py'])
    
    exe1.wait()
    

    I wanted to post the answer just in case this is helpful to anyone.