0

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.

Iwan
  • 309
  • 1
  • 6
  • 17
  • 1
    You have to call it the same way you initialize python from the command line. `py C:\Users\User\Documents\get summary into folder.py` (or the `python` command depending on how you have it configured. – FelisPhasma Nov 02 '17 at 14:50

1 Answers1

1

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)
BPL
  • 9,632
  • 9
  • 59
  • 117
  • 1
    Hi, Thanks for that. I am a bit of a newbie though; where do the file destinations go in your example? – Iwan Nov 02 '17 at 15:21