简体   繁体   中英

Passing command line argument to subprocess module

I have a python script which calls perl script using subprocess module.

In terminal i run the perl script like this

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

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. 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.

Above is a rough explanation and only works in Windows. For more detail, see @ShadowRanger 's comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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