簡體   English   中英

依次運行多個python腳本

[英]Running multiple python scripts in a sequence

我想按順序執行腳本,每個腳本之間都有時間延遲。

目的是運行腳本,該腳本掃描文件名中的字符串並將這些文件導入文件夾。 時間延遲是為了讓腳本有時間在移至下一個文件之前完成文件的復制。

我已經嘗試過在Stackoverflow上提出的問題:

運行多個Python腳本

從另一個python腳本運行python腳本,並傳入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)

該腳本會打開文件,但不會運行。

首先, 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM