Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hot to call Perl script from Python 3

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
US
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.

[sub]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])
[/sub]
 
Hi Tester_V,
You can use os.popen() like in this example:

tester_v.pl
Code:
[COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$#ARGV[/color]  != [COLOR=#ff00ff]0[/color]) {
  [COLOR=#804040][b]die[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Usage:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]  [perl] [/color][COLOR=#008080]$0[/color][COLOR=#ff00ff] <arg>[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
}
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Now executing Perl:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]  Hello from Perl [/color][COLOR=#008080]$ARGV[[/color][COLOR=#ff00ff]0[/color][COLOR=#008080]][/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]End Of Perl.[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

tester_v.py
Code:
[COLOR=#a020f0]import[/color] os, string
my_cmd = [COLOR=#ff00ff]"perl tester_v.pl Tester_V"[/color]
my_cmd_output = os.popen(my_cmd)
[COLOR=#804040][b]for[/b][/color] line [COLOR=#804040][b]in[/b][/color] my_cmd_output:
  [COLOR=#008080]print[/color](line.rstrip())
[COLOR=#008080]print[/color]([COLOR=#ff00ff]"Done."[/color])

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

It definitely depends on how much control over the subprocess you need, but just to run it a single [tt]subprocess.call()[/tt] is enough :
Code:
[blue]bash-5.0$[/blue] cat Tester_V.py
import sys, subprocess

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

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

[blue]bash-5.0$[/blue] 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
[ul]
[li]How the Perl script works : Does it expect input ? Does it expect multiple inputs after some specific prompts ?[/li]
[li]What do you need from the Perl script : Exit code ? Output ? Error ?[/li]
[li]What "does not work" means : Crash ? No result ? Wrong result ?[/li]
[/ul]


Feherke.
feherke.github.io
 
You guys great! I really appreciate your help!
 
Perhaps those guys, being greatly helpful, deserve a "Great Post" vote?

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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top