【问题标题】:os.system (old python) and arguments with parametersos.system(旧python)和带参数的参数
【发布时间】:2012-03-19 10:26:43
【问题描述】:

我试图编写简单的代码来执行带参数的 os 命令

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

target = "i586"

build = os.system('/usr/bin/hsh --target="target"')

但它总是以 /usr/bin/hsh --target=target 而不是 target=i586 开头。 也 subprocess.call 不工作导致 python 太旧。

请帮帮我。

【问题讨论】:

  • '"target"' 是一个包含八个字符的字符串"target",而不是变量target 的内容。
  • 请将您的 Python 版本添加到问题中

标签: python os.system


【解决方案1】:
build = os.system('/usr/bin/hsh --target="%s"' % target)

build = os.system('/usr/bin/hsh --target="' + target + '"')

【讨论】:

  • Nice.Thanks.But 如果我想要多个参数,我必须构建 = os.system('/usr/bin/hsh --target="%s" --mountpoints="%s "' % 目标,挂载点) ?
  • 是的,但是在 args 周围加上括号:str % (arg1, arg2, arg3)
  • 对于形成命令的多个参数首先很容易理解,cmd = '/usr/bin/hsh --target="%s, %s, %s"' %(target1, target2, target3) 然后build = os.system(cmd)
猜你喜欢
  • 1970-01-01
  • 2013-09-01
  • 2013-01-31
  • 1970-01-01
  • 2017-06-19
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多