Run a perl script from my python script, print the output and wait for it to complete

I have a python script that is required at some point to run a perl script, wait for it to complete and continue.

Since this case will only happen on a Windows machine, I thought I could just open a new cmd and run a perl script, but I am having difficulty getting it.

import os

os.system("start /wait cmd /c {timeout 10}")

      

should open a new cmd and sleep for 10 seconds but it closes immediately. I don't want to put a perl script in position timeout 10

as it is quite resource intensive. Another idea was to use subprocess

with call

or Popen

and wait

.

perl_script = subprocess.call(['script.pl', params])

      

But I'm not sure what will happen to the stdout perl script in that case.

I know the location and parameters of the perl script.

How can I run a perl script from my python script, print the output (a lot) and wait for it to complete?

change

As suggested by @rchang I added subprocess

with the communicate

following: it works as expected.

import subprocess, sys

perl = "C:\\perl\\bin\\perl.exe"
perl_script "C:\\scripts\\perl\\flamethrower.pl"
params = " --mount-doom-hot"

pl_script = subprocess.Popen([perl, perl_script, params], stdout=sys.stdout)
pl_script.communicate()

      

These are my first lines of perl, just a quick copy / past script to test this.

print "Hello Perld!\n";
sleep 10;
print "Bye Perld!\n";

      

+3


source to share


2 answers


import subprocess
import sys

perl_script = subprocess.Popen(["script.pl", params], stdout=sys.stdout)
perl_script.communicate()

      



This should connect the stdout of the subprocess to the stdout stream of the Python script, unless you really need the Python script to output anything meaningful at runtime that cannot be linked to the subprocess's output.

+4


source


You may try:



perl_script = subprocess.check_output(["script.pl", params])
print perl_script

      

+1


source







All Articles