简体   繁体   English

从 python 运行 perl 脚本

[英]Run perl script from python

I know there are some topic on Stack Overflow about this.我知道 Stack Overflow 上有一些关于这个的话题。 But none of these make any sense to me.但这些对我来说都没有任何意义。 I am new to both python and perl and trying my best to understand.我是 python 和 perl 的新手,并尽力理解。 I would like to run a perl script from a piece of python code.我想从一段 python 代码运行一个 perl 脚本。

executing the perl script in command prompt goes as following:在命令提示符下执行 perl 脚本如下:

perl perlscript.pl input.bopt7 output.xml

I would like to run this command from my python code.我想从我的 python 代码运行这个命令。 I have tried the following:我尝试了以下方法:

pipe = subprocess.Popen(["perlscript.pl" , "input.bopt7" , "output.xml"], stdout=subprocess.PIPE)

but this does not work.但这不起作用。 I get an error saying it is not a valid win32 ...我收到一条错误消息,说它不是有效的 win32 ...

I need no input or output from this script.我不需要这个脚本的输入或输出。 Just need to run it once.只需要运行一次。

You need to include the perl command itself when executing a perl script:执行 perl 脚本时,您需要包含perl命令本身:

pipe = subprocess.Popen(["perl", "perlscript.pl" , "input.bopt7" , "output.xml"], stdout=subprocess.PIPE)

You did the same thing on the command line prompt;你在命令行提示符下做了同样的事情; the Popen class cannot guess from the perlscript.pl file that you wanted to run this script with Perl.Popen类不能从猜测perlscript.pl文件,你想运行此脚本用Perl。 :-) :-)

Did you try to add perl to Popen arguments (just as you do on the command line)?您是否尝试将 perl 添加到 Popen 参数(就像您在命令行上所做的那样)?

pipe = subprocess.Popen(["perl", "perlscript.pl" , "input.bopt7" , "output.xml"], stdout=subprocess.PIPE) pipe = subprocess.Popen(["perl", "perlscript.pl", "input.bopt7", "output.xml"], stdout=subprocess.PIPE)

In your example, Windows tries to execute "perlscript.pl" as a Win32 executable, since this is the first parameter you specified, and fails because it doesn't contain the proper binary header (since it is a text file).在您的示例中,Windows 尝试将“perlscript.pl”作为 Win32 可执行文件执行,因为这是您指定的第一个参数,但失败是因为它不包含正确的二进制标头(因为它是一个文本文件)。

The first argument should be perl.exe , if perl.exe is in your PATH ;第一个参数应该是perl.exe ,如果perl.exe在你的PATH or the full path to the executable as all the rest are arguments to perl.exe .或可执行文件的完整路径,因为其余的都是perl.exe参数。

Also make sure you put the full path for perlscript.pl and input.bopt7 .还要确保为perlscript.plinput.bopt7放置完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM