将变量传递给子流程调用

发布于 2024-10-14 01:42:44 字数 595 浏览 2 评论 0原文

我正在尝试将变量从 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"

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

#!/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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谁许谁一生繁华 2024-10-21 01:42:44

您还可以使用 f 字符串。

 import subprocess

 site = 'google.com'
 subprocess.run(['nslookup', f'{site}'])

You can also use f-strings.

 import subprocess

 site = 'google.com'
 subprocess.run(['nslookup', f'{site}'])
变身佩奇 2024-10-21 01:42:44

尝试用逗号分隔值:

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

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

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

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.

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.

眼泪也成诗 2024-10-21 01:42:44

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

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

subprocess.call 采用与 subprocess.Popen

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


编辑

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

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

Try to add commas between your list items:

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

subprocess.call takes the same arguments as subprocess.Popen:

args should be a string, or a sequence of program arguments.


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 ]
因为看清所以看轻 2024-10-21 01:42:44

您可以先创建 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)

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

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)
沙与沫 2024-10-21 01:42:44
subprocess.call(['useradd', '-m','-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username])

list 传递给 subprocess.call

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

Pass a list to subprocess.call

呢古 2024-10-21 01:42:44

您是否尝试过仅使用 'xxx{0}'.format(variable)
参见链接1 或 link2

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

或者

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

对我来说效果很好

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文