简体   繁体   中英

Wait for subprocess .exe to finish before proceeding in Python

I'm running an application from within my code, and it rewrites files which I need to read later on in the code. There is no output the goes directly into my program. I can't get my code to wait until the subprocess has finished, it just goes ahead and reads the unchanged files.

I've tried subprocess.Popen.wait(), subprocess.call(), and subprocess.check_call(), but none of them work for my problem. Does anyone have any idea how to make this work? Thanks.

Edit: Here is the relevant part of my code:

    os.chdir('C:\Users\Jeremy\Documents\FORCAST\dusty')
    t = subprocess.Popen('start dusty.exe', shell=True)
    t.wait()      
    os.chdir('C:\Users\Jeremy\Documents\FORCAST')

Do you use the return object of subprocess.Popen()?

p = subprocess.Popen(command)
p.wait()

should work.

Are you sure that the command does not end instantly?

If you execute a program with

t = subprocess.Popen(prog, Shell=True)

Python won't thrown an error, regardless whether the program exists or not. If you try to start an non-existing program with Popen and Shell=False, you will get an error. My guess would be that your program either doesn't exist in the folder or doesn't execute. Try to execute in the Python IDLE environment with Shell=False and see if you get a new window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM