Python 将变量传递给子进程调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4795190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:33:39  来源:igfitidea点击:

Passing variables to a subprocess call

pythonsubprocess

提问by rsouthard

I am trying to pass my variables from raw_inputto my subprocess command. I am new to Python. Any help would he appreciated.

我试图将我的变量传递raw_input给我的子进程命令。我是 Python 的新手。任何帮助他将不胜感激。

#!/usr/bin/python

import subprocess

print "\nWhat user name"
username = str(raw_input('username: '))
print "\nWhat is the user id"
userid = int(raw_input('Enter user id: '))
print "\nWhat is the user\'s primary group?"
primarygroup = int(raw_input('Enter group: '))
print "\nWhat is the user\'s secondary group?"
secondarygroup = int(raw_input('Enter group: '))

subprocess.call(['useradd' '-m' '-g' _primarygroup '-G' _secondarygroup '-u' _userid _username])

print"\nThe user has been added"

回答by ThiefMaster

Try separating the values with commas:

尝试用逗号分隔值:

subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username])

See http://docs.python.org/library/subprocess.html#subprocess.call- It takes an array where the first argument is the program and all other arguments are passed as arguments to the program.

请参阅http://docs.python.org/library/subprocess.html#subprocess.call- 它采用一个数组,其中第一个参数是程序,所有其他参数作为参数传递给程序。

Also don't forget to check the return value of the function for a zero return code which means "success" unless it doesn't matter for your script if the user was added successfully or not.

另外不要忘记检查函数的返回值是否为零返回代码,这意味着“成功”,除非用户是否成功添加对您的脚本无关紧要。

回答by miku

Try to add commas between your list items:

尝试在列表项之间添加逗号:

subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, \
                 '-u' ,_userid, _username])

subprocess.calltakes the same arguments as subprocess.Popen:

subprocess.call采用与 相同的参数subprocess.Popen

argsshould be a string, or a sequenceof program arguments.

args应该是一个字符串,或一系列程序参数。



Edit

编辑

To turn all your arguments into strings at once you could you a list comprehension:

要将所有参数一次转换为字符串,您可以使用列表理解:

args = ['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, \
        '-u' ,_userid, _username]
str_args = [ str(x) for x in args ]

回答by user225312

subprocess.call(['useradd', '-m','-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username])

Pass a listto subprocess.call

传递listsubprocess.call

回答by Daniel Luevano Alonso

You can create the arg string first and then just past this variable to the subprocess.call. For example:

您可以先创建 arg 字符串,然后将此变量传递给 subprocess.call。例如:

projects_csv_fn = 'projects_csv_2.csv'
prjects_json_fn = 'projects.json'

args ='python json_to_csv.py id ' + prjects_json_fn + ' ' + projects_csv_fn

subprocess.call(args, shell=True)

回答by Javi

have you tried just using 'xxx{0}'.format(variable)? see link1or link2

你试过只使用'xxx{0}'.format(variable)吗? 见链接 1或链接 2

subprocess.run(['useradd', '-m', '-g {0}'.format(_primarygroup), '-G {0}'.format(_secondarygroup), '-u {0}'.format(_userid)], capture_output=True)

or

或者

subprocess.call(['useradd', '-m', '-g {0}'.format(_primarygroup), '-G {0}'.format(_secondarygroup), '-u {0}'.format(_userid)])

just worked fine to me

对我来说很好用