简体   繁体   中英

Running multiple python scripts in a sequence

I have scripts I would like to execute in sequence with a time delay between the each of them.

The intention is to run the scripts which scan for an string in file names and imports those files into a folder. The time delay is to give the script the time to finish copying the files before moving to the next file.

I have tried the questions already posed on Stackoverflow:

Running multiple Python scripts

Run a python script from another python script, passing in args

But I'm not understanding why the lines below don't work.

import time
import subprocess

subprocess.call(r'C:\Users\User\Documents\get summary into folder.py', shell=True)
time.sleep(100)
subprocess.call(r'C:\Users\User\Documents\get summaries into folder.py', shell=True)
time.sleep(100)

The script opens the files but doesn't run.

Couple of things, first of all, time.sleep accepts seconds as an argument, so you're waiting 100s after you've spawned these 2 processes, I guess you meant .100 . Anyway, if you just want to run synchronously your 2 scripts better use subprocess.Popen.wait , that way you won't have to wait more than necessary, example below:

import time
import subprocess

test_cmd = "".join([
    "import time;",
    "print('starting script{}...');",
    "time.sleep(1);",
    "print('script{} done.')"
])

for i in range(2):
    subprocess.Popen(
        ["python", "-c", test_cmd.format(*[str(i)] * 2)], shell=True).wait()
    print('-'*80)

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