首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从Perl调用Python脚本

从Perl调用Python脚本
EN

Stack Overflow用户
提问于 2018-07-10 04:16:04
回答 2查看 989关注 0票数 -1

我正在尝试在perl中捕获python exit coode表单。

我的python代码py_test.py:

代码语言:javascript
复制
import sys
sys.exit(sys.argv[1])

我的perl代码test.pl:

代码语言:javascript
复制
#!/usr/bin/perl -w
$res = system("python py_test.py ".$ARGV[0]);
print("\$res ==== $res \n >>>". $? ."\n");

我跑了一下:

代码语言:javascript
复制
perl test.pl 4

结果是:

代码语言:javascript
复制
4
$res ==== 256
>>>256

我在32位RHEL 5.3上使用python 2.4.3和perl 5.8.8。

有人能帮我理解这里发生了什么吗,我似乎不能理解这种行为,我做错了什么吗?(我看了这段代码几个小时,我从来没有遇到过系统这么奇怪的事情)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-10 13:40:56

我找到我的问题了。我输入了sys.exit argv1,但在开始时python将其视为字符串。在我把它改成sys.exit(int(argv1))之后,一切都运行得很好。

这也解释了为什么它打印了"4"...

票数 1
EN

Stack Overflow用户

发布于 2018-07-10 07:09:27

来自"perldoc perlvar“

代码语言:javascript
复制
$?      The status returned by the last pipe close, backtick (``) command,
        successful call to "wait()" or "waitpid()", or from the "system()"
        operator. This is just the 16-bit status word returned by the
        traditional Unix "wait()" system call (or else is made up to look
        like it). Thus, the exit value of the subprocess is really ("$? >>
        8"), and "$? & 127" gives which signal, if any, the process died
        from, and "$? & 128" reports whether there was a core dump.

听起来您的python脚本可能无法成功退出4。

test.py

代码语言:javascript
复制
#!/usr/bin/env python3

import sys

sys.exit(4)

test.pl

代码语言:javascript
复制
#!/usr/bin/env perl
use warnings;
use strict;

system("./test.py");
print "\$? = $?\n"
die("Killed by signal ".( $? & 0x7F ) if $? & 0x7F;
die("Exited with error ".( $? >> 8 ) if $? >> 8;
print "Success\n";

结果:

代码语言:javascript
复制
~/test$ ./test.pl
$? = 1024
Exited with error 4
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51253378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档