从 Python 调用 Perl 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4682088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:54:47  来源:igfitidea点击:

Call Perl script from Python

python

提问by user574490

I've got a Perl script that I want to invoke from a Python script. I've been looking all over, and haven't been successful. I'm basically trying to call the Perl script sending 1 variable to it, but don't need the output of the Perl script, as it is a self contained program.

我有一个 Perl 脚本,我想从 Python 脚本中调用它。我到处找,都没有成功。我基本上是在尝试调用 Perl 脚本,向它发送 1 个变量,但不需要 Perl 脚本的输出,因为它是一个自包含程序。

What I've come up with so far is:

到目前为止我想出的是:

var = "/some/file/path/"
pipe = subprocess.Popen(["./uireplace.pl", var], stdin=subprocess.PIPE)
pipe.stdin.write(var)
pipe.stdin.close()

Only just started Python programming, so I'm sure the above is total nonsense. Any help would be much appreciated.

才刚刚开始 Python 编程,所以我确定上面的内容完全是胡说八道。任何帮助将非常感激。

采纳答案by Ken Kinder

If you just want to open a pipe to a perl interpreter, you're on the right track. The only thing I think you're missing is that the perl script itself is not an executable. So you need to do this:

如果您只想打开一个通向 perl 解释器的管道,那么您就走对了。我认为您唯一缺少的是 perl 脚本本身不是可执行文件。所以你需要这样做:

var = "/some/file/path/"
pipe = subprocess.Popen(["perl", "./uireplace.pl", var], stdin=subprocess.PIPE)
pipe.stdin.write(var)
pipe.stdin.close()

回答by Donovan

I Hope thiscan help you. Do not know how to do that otherwise.

我希望可以帮助你。否则不知道该怎么做。

回答by Sven Marnach

Would you like to pass varas a parameter, on stdin or both? To pass it as a parameter, use

你想var作为参数传递,在标准输入上还是两者兼而有之?要将其作为参数传递,请使用

subprocess.call(["./uireplace.pl", var])

To pipe it to stdin, use

要将其通过管道传输到标准输入,请使用

pipe = subprocess.Popen("./uireplace.pl", stdin=subprocess.PIPE)
pipe.communicate(var)

Both code snippets require uireplace.plto be executable. If it is not, you can use

两个代码片段都需要uireplace.pl可执行。如果不是,您可以使用

pipe = subprocess.Popen(["perl", "./uireplace.pl"], stdin=subprocess.PIPE)
pipe.communicate(var)

回答by bedwyr

You could try the subprocess.call()method. It won't return output from the command you're invoking, but rather the return code to indicate if the execution was successful.

您可以尝试subprocess.call()方法。它不会返回您正在调用的命令的输出,而是返回代码以指示执行是否成功。

var = "/some/file/path"
retcode = subprocess.call(["./uireplace.pl", var])
if retcode == 0:
    print("Passed!")
else:
    print("Failed!")

Make sure you're Perl script is executable. Otherwise, you can include the Perl interpreter in your command (something like this):

确保你的 Perl 脚本是可执行的。否则,您可以在命令中包含 Perl 解释器(类似这样):

subprocess.call(["/usr/bin/perl", "./uireplace.pl", var])

回答by mouad

Just do:

做就是了:

var = "/some/file/path/"
pipe = subprocess.Popen(["perl", "uireplace.pl", var])