×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Hot to call Perl script from Python
3

Hot to call Perl script from Python

Hot to call Perl script from Python

(OP)
Hi,
there are a lot of snippets on how to do this, I probably tried 90% of them by now but nothing is working.
I started writing in Python, I never thought such a simple thing like a calling other script would be that complicated, what a pile of crap this Python is.
Any way, is anybody has a code I can use?
Thank you.

Here is a code and at the bottom of the code is a "perl call" that does not work.

import os
import socket
import sys


pfile = open('C:/Scripts/CMV_Hosts_Verification/Lists/IP_Active.txt')

h_active = open("C:/Scripts/CMV_Hosts_Verification/Lists/CMV_Active_Hosts_List.txt","w")
sys.stdout = h_active
while True:
IP = pfile.readline()
try:
host = socket.gethostbyaddr(IP.rstrip())
print(IP,host)
print (IP,file=h_active)
except EOFError:
print('Why did you do an EOF on me?',file=h_active)

pflie.close()
h_active.close()


var = "C:/Scripts/CMV_Hosts_Verification/"
pipe = subprocess.Popen(["perl", "Clean_Host_Names_CMV.pl"])
subprocess.call(["C:/Strawberry/perl/bin/.perl.exe", "C:/Scripts/CMV_Hosts_Verification/Clean_Host_Names_CMV.pl", var])

RE: Hot to call Perl script from Python

Hi Tester_V,
You can use os.popen() like in this example:

tester_v.pl

CODE

if ($#ARGV  != 0) {
  die "Usage:\n  [perl] $0 <arg>\n";
}
print "Now executing Perl:\n";
print "  Hello from Perl $ARGV[0] !\n";
print "End Of Perl.\n"; 

tester_v.py

CODE

import os, string
my_cmd = "perl tester_v.pl Tester_V"
my_cmd_output = os.popen(my_cmd)
for line in my_cmd_output:
  print(line.rstrip())
print("Done.") 

Output:

CODE

$ python3 tester_v.py
Now executing Perl:
  Hello from Perl Tester_V !
End Of Perl.
Done. 

RE: Hot to call Perl script from Python

Hi

It definitely depends on how much control over the subprocess you need, but just to run it a single subprocess.call() is enough :

CODE --> command line

bash-5.0$ cat Tester_V.py
import sys, subprocess

subprocess.call(['/usr/bin/perl', 'Tester_V.pl', 'foo', 'bar', *sys.argv[1:]])

bash-5.0$ cat Tester_V.pl
print "I'm Perl $], and you want me to handle '@ARGV'.\n";

bash-5.0$ python3 Tester_V.py fiz baz
I'm Perl 5.028001, and you want me to handle 'foo bar fiz baz'. 

Please help us to help you by providing further information about
  • How the Perl script works : Does it expect input ? Does it expect multiple inputs after some specific prompts ?
  • What do you need from the Perl script : Exit code ? Output ? Error ?
  • What "does not work" means : Crash ? No result ? Wrong result ?

Feherke.
feherke.github.io

RE: Hot to call Perl script from Python

(OP)
You guys great! I really appreciate your help!

RE: Hot to call Perl script from Python

Perhaps those guys, being greatly helpful, deserve a "Great Post" vote?

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close