通过Python获取Perl脚本的控制台输出

时间:2015-01-29 21:02:48

标签: python perl

有各种各样的帖子和资源解释了如何使用Python来获取外部呼叫的输出。我熟悉使用这些 - 我已经使用Python来获取jar和exec的输出几次,当在Python本身内重新实现jar / exec的功能是不现实或经济的时候。

我试图通过Python的subprocess模块调用Perl脚本,但是我对这个特定的Perl脚本没有成功。我仔细地按照这里的答案Call Perl script from Python,但没有结果。

我能够从这个问题/答案中获取此测试Perl脚本的输出:How to call a Perl script from Python, piping input to it?

#!/usr/bin/perl

use strict;
use warnings;

my $name = shift;

print "Hello $name!\n";

使用这段Python代码:

import subprocess

var = "world"

args_test = ['perl', 'perl/test.prl', var]

pipe = subprocess.Popen(args_test, stdout=subprocess.PIPE)

out, err = pipe.communicate()

print out, err

但是,如果我将参数和Perl脚本替换为我需要输出的那个,我根本就没有输出。

args = ['perl', 'perl/my-script.prl', '-a', 'perl/file-a.txt',
         '-t', 'perl/file-t.txt', 'input.txt']

在命令行输入时正确运行,例如

>perl perl/my-script.prl -a perl/file-a.txt -t perl/file-t.txt input.txt

但是当通过子进程调用时,这不会产生输出:

pipe = subprocess.Popen(args, stdout=subprocess.PIPE)

out, err = pipe.communicate()

print out, err

我也做过另一次健全检查。这正确地将Perl的帮助消息输出为字符串:

import subprocess

pipe = subprocess.Popen(['perl', '-h'], stdout=subprocess.PIPE)

out, err = pipe.communicate()

print out, err

如下所示:

>>> ================================ RESTART ================================
>>>

Usage: perl [switches] [--] [programfile] [arguments]
  -0[octal]       specify record separator (\0, if no argument)
  -a              autosplit mode with -n or -p (splits $_ into @F)
  -C[number/list] enables the listed Unicode features
  -c              check syntax only (runs BEGIN and CHECK blocks)
  -d[:debugger]   run program under debugger
  -D[number/list] set debugging flags (argument is a bit mask or alphabets)
  -e program      one line of program (several -e's allowed, omit programfile)
  -f              don't do $sitelib/sitecustomize.pl at startup
  -F/pattern/     split() pattern for -a switch (//'s are optional)
  -i[extension]   edit <> files in place (makes backup if extension supplied)
  -Idirectory     specify @INC/#include directory (several -I's allowed)
  -l[octal]       enable line ending processing, specifies line terminator
  -[mM][-]module  execute "use/no module..." before executing program
  -n              assume "while (<>) { ... }" loop around program
  -p              assume loop like -n but print line also, like sed
  -P              run program through C preprocessor before compilation
  -s              enable rudimentary parsing for switches after programfile
  -S              look for programfile using PATH environment variable
  -t              enable tainting warnings
  -T              enable tainting checks
  -u              dump core after parsing program
  -U              allow unsafe operations
  -v              print version, subversion (includes VERY IMPORTANT perl info)
  -V[:variable]   print configuration summary (or a single Config.pm variable)
  -w              enable many useful warnings (RECOMMENDED)
  -W              enable all warnings
  -x[directory]   strip off text before #!perl line and perhaps cd to directory
  -X              disable all warnings

None

0 个答案:

没有答案