Reputation: 65
import subprocess
result=subprocess.Popen(['perl','Hello.pl'],stdout=subprocess.PIPE,shell=True)
out,err=result.communicate()
print out
This is my program , i am trying to run a perl
program inside a python file . I am using python 2.6v
.
While running this file it's not giving anything .
I am new to python .
Can anyone help ?
Upvotes: 0
Views: 1015
Reputation: 2943
From this documentation for communicate()
:
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.
Don't use shell=True
, if you want to write and read without waiting for the process to stop. Refer this doc for details.
Use this code:
import subprocess
result=subprocess.Popen(['perl','Hello.pl'],stdout=subprocess.PIPE)
out,err=result.communicate()
print out
How can i read some arguments from command line and pass as an input . For Ex : perl Hello.pl some_variable_name
You can do that using argparse
Python code:
import subprocess
import argparse
parser = argparse.ArgumentParser(description='Test Python Code')
parser.add_argument('first_name', metavar='FIRST_NAME', type=str,
help='Enter the first name')
parser.add_argument('second_name', metavar='SECOND_NAME', type=str,
help='Enter the second name')
# Parse command line arguments
args = parser.parse_args()
result=subprocess.Popen(['perl','Hello.pl',args.first_name,args.second_name],stdout=subprocess.PIPE)
out,err=result.communicate()
print out
Perl Code:
#!/usr/bin/perl -w
# (1) quit unless we have the correct number of command-line args
$num_args = $#ARGV + 1;
if ($num_args != 2) {
print "\nUsage: name.pl first_name last_name\n";
exit;
}
# (2) we got two command line args, so assume they are the
# first name and last name
$first_name=$ARGV[0];
$last_name=$ARGV[1];
print "First name: $first_name \nSecond name: $last_name\n";
Upvotes: 2