【问题标题】:python subprocess give typeerrorpython子进程给出类型错误
【发布时间】:2013-12-04 04:41:18
【问题描述】:

我正在使用 python 2.7

cmd = "sudo start service/newservice db=temp"
p = subprocess.Popen(shlex.split(cmd), stderr=subprocess.STDOUT)

上面的命令工作正常,并且产生了预期的过程。

cmd = "sudo stop service/newservice db=temp"
p = subprocess.Popen(shlex.split(cmd), stderr=subprocess.STDOUT)

调用上述方法时在同一个文件中。它给出了错误。

traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
        errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1213, in _execute_child
        raise child_exception
    TypeError: must be encoded string without NULL bytes, not str

【问题讨论】:

标签: python python-2.7 subprocess


【解决方案1】:

我解决了。我只在将 cmd 传递给 shlex.split 之前将其转换为 str,这很好,尤其是当您以这种方式接收一些输入时:

cmd = "sudo stop service/newservice db=" + db

更安全的做法是将其转换为字符串。

【讨论】:

  • 请在其中使用格式说明符。例如。 cmd = "sudo stop service/newservice db=%s"%str(db)
猜你喜欢
  • 2012-10-08
  • 2015-05-31
  • 1970-01-01
  • 2017-03-05
  • 2016-06-17
  • 2016-10-08
  • 1970-01-01
  • 2018-03-01
  • 2015-12-03
相关资源
最近更新 更多