简体   繁体   English

从perl运行python脚本,带有stdin参数并保存stdout输出

[英]Running python script from perl, with argument to stdin and saving stdout output

My perl script is at path: 我的perl脚本位于路径:

a/perl/perlScript.pl

my python script is at path: 我的python脚本位于路径:

a/python/pythonScript.py

pythonScript.py gets an argument from stdin, and returns result to stdout. pythonScript.py从stdin获取一个参数,并将结果返回到stdout。 From perlScript.pl , I want to run pythonScript.py with the argument hi to stdin, and save the results in some variable. perlScript.pl ,我想将pythonScript.pyhi一起运行,并将结果保存到某个变量中。 That's what I tried: 那就是我尝试过的:

my $ret = `../python/pythonScript.py < hi`;                   

but I got the following error: 但出现以下错误:

The system cannot find the path specified.            

Can you explain the path can't be found? 您能否解释找不到的路径?

The qx operator (backticks) starts a shell ( sh ), in which prog < input syntax expects a file named input from which it will read lines and feed them to the program prog . qx运算符 (backticks)启动外壳程序( sh ),其中prog < input语法期望一个名为input 的文件 ,该文件将从其中读取行并将其馈送到程序prog But you want the python script to receive on its STDIN the string hi instead, not lines of a file named hi . 但是您希望python脚本在其STDIN上接收字符串hi而不是名称为hi的文件的行。

One way is to directly do that, my $ret = qx(echo "hi" | python_script) . 一种方法是直接执行此操作,即my $ret = qx(echo "hi" | python_script)

But I'd suggest to consider using modules for this. 但是我建议考虑为此使用模块。 Here is a simple example with IPC::Run3 这是IPC :: Run3的简单示例

use warnings;
use strict;
use feature 'say';

use IPC::Run3;

my @cmd = ('program', 'arg1', 'arg2');

my $in = "hi";

run3 \@cmd, \$in, \my $out;

say "script's stdout: $out";

The program is the path to your script if it is executable, or perhaps python script.py . program是脚本的可执行文件的路径,或者是python script.py This will be run by system so the output is obtained once that completes, what is consistent with the attempt in the question. 这将由system运行,以便在完成后获得输出,这与问题中的尝试一致。 See documentation for module's operation. 有关模块的操作,请参见文档。

This module is intended to be simple while " satisfy 99% of the need for using system , qx , and open3 [...] . For far more power and control see IPC::Run . 此模块旨在简化,同时“ 满足使用systemqxopen3 [...] 99%的需求 。有关更多功能和控制,请参阅IPC :: Run

You're getting this error because you're using shell redirection instead of just passing an argument 之所以收到此错误,是因为您使用的是Shell重定向,而不仅仅是传递参数

../python/pythonScript.py < hi

tells your shell to read input from a file called hi in the current directory, rather than using it as an argument. 告诉您的Shell从当前目录中名为hi的文件中读取输入,而不是将其用作参数。 What you mean to do is 你的意思是

my $ret = `../python/pythonScript.py hi`; 

Which correctly executes your python script with the hi argument, and returns the result to the variable $ret . 它可以使用hi参数正确执行您的python脚本,并将结果返回到变量$ret

The Some of the other answers assume that hi must be passed as a command line parameter to the Python script but the asker says it comes from stdin. 一些其他的答案的假设hi必须作为命令行参数的Python脚本传递,但提问者说,这是来自标准输入。

Thus: 从而:

my $ret = `echo "hi" | ../python/pythonScript.py`;

To launch your external script you can do 要启动外部脚本,您可以执行

system "python ../python/pythonScript.py hi";

and then in your python script 然后在你的python脚本中

import sys
def yourFct(a, b):
    ...

if __name__== "__main__":
    yourFct(sys.argv[1])

you can have more informations on the python part here 您可以在此处获得有关python部分的更多信息

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

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