【问题标题】:error when running call() in Python subprocess在 Python 子进程中运行 call() 时出错
【发布时间】:2013-01-06 13:52:09
【问题描述】:

我正在尝试运行:

 try:
    with open(subprocess.PIPE, 'w') as pipe:
          call(["/usr/sbin/atms","-k"], stdout=pipe, stderr=pipe)                                        
          call(["/usr/sbin/atms","/usr/sbin/atms.conf"],stdout=pipe,stder=pipe)
 except Exception, e:
          print e

我现在明白了

 coercing to Unicode: need string or buffer, int found

什么意思?

谢谢

【问题讨论】:

  • 不需要使用os.devnull来重定向stdout或err。只需使用subprocess.PIPE 并忽略输出。
  • 好的..会试试..是不是要把代码中的那一行替换成subprocess.PIPE而不是os.devnull?
  • 完全正确;如果您可以通过管道忽略,则无需重定向到/dev/null。我并不是说这会解决你的问题,只是说使用 /dev/null 是一个 shell 解决方案,这就是 Python。
  • @MartijnPieters 我试过了并在上面添加了结果.. :-(
  • 您确实不需要需要将PIPE 作为文件打开。使用stdout=subprocess.PIPE,请参阅文档。 :-)

标签: python linux dev-null


【解决方案1】:

open() 用于文件,需要文件名而不是管道。

您可以使用Popen,而不是.call()

>>> p = subprocess.Popen(['python', '-c', 'print "test"'], stdout=subprocess.PIPE)
>>> p.stdout.read()
'test\r\n'

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 2021-07-24
    • 2016-12-20
    • 2012-10-08
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多