简体   繁体   English

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

[英]Passing command line argument to subprocess module

I have a python script which calls perl script using subprocess module. 我有一个python脚本,它使用子进程模块调用perl脚本。

In terminal i run the perl script like this 在终端中,我像这样运行perl脚本

perl email.pl raj@gmail.com

I am passing in raj@email.com as command line argument to that script 我传入raj@email.com作为该脚本的命令行参数

This is my Python script 这是我的Python脚本

import subprocess

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

This works fine 这很好

But if i pass arguments it throws file not found 但是,如果我传递参数,则找不到文件

import subprocess

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

error: 错误:

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

How could i pass command line arguments in this case? 在这种情况下,如何传递命令行参数?

The command can be a string: 该命令可以是字符串:

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

or a list: 或清单:

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

When it is a list, Python will escape special chars. 当它是列表时,Python将转义特殊字符。 So when you say 所以当你说

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

It calls 它呼吁

perl "./email.pl moun"

But the file "email.pl moun" doesn't exist. 但是文件“ email.pl moun”不存在。

Above is a rough explanation and only works in Windows. 以上是一个粗略的解释,仅适用于Windows。 For more detail, see @ShadowRanger 's comment. 有关更多详细信息,请参见@ShadowRanger的评论。

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

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