perl 将命令行参数传递给子进程模块

vulvrdjw  于 2022-12-19  发布在  Perl
关注(0)|答案(1)|浏览(135)

我有一个Python脚本,它使用subprocess模块调用Perl脚本。

perl email.pl raj@gmail.com

I am passing in "raj@email.com" as command line argument to that script. This is my Python script:

import subprocess

pipe = subprocess.Popen(["perl","./email.pl"])
print pipe

这很好用,但是如果我传递参数,它会抛出file not found:

import subprocess

pipe = subprocess.Popen(["perl","./email.pl moun"])
print pipe

错误:

<subprocess.Popen object at 0x7ff7854d6550>
Can't open perl script "./email.pl moun": No such file or directory

在这种情况下,如何传递命令行参数?

rjee0c15

rjee0c151#

该命令可以是字符串:

pipe = subprocess.Popen("perl ./email.pl moun")

或列表:

pipe = subprocess.Popen(["perl", "./email.pl", "moun"])

当它是一个列表时,Python会转义特殊字符。

pipe = subprocess.Popen(["perl","./email.pl moun"])

它叫

perl "./email.pl moun"

但是文件“email.pl moun”不存在。
以上是一个粗略的解释,只适用于Windows。更多细节,请参见@ShadowRanger的评论。

相关问题