按顺序运行多个python脚本

时间:2017-11-02 14:31:39

标签: python

我想要按顺序执行脚本,每个脚本之间有一段时间延迟。

目的是运行扫描文件名中字符串的脚本,并将这些文件导入文件夹。时间延迟是让脚本有时间完成复制文件,然后再移动到下一个文件。

我已经尝试过在Stackoverflow上提出的问题:

  

Running multiple Python scripts

     

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

但我不明白为什么下面的行不起作用。

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)

该脚本会打开文件,但不会运行。

1 个答案:

答案 0 :(得分:1)

首先,time.sleep接受秒作为参数,所以你在产生这两个过程后等待了100秒,我想你的意思是.100。无论如何,如果您只想同步运行2个脚本,请更好地使用subprocess.Popen.wait,这样您就不必等待超过必要的时间,例如:

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)
相关问题