【发布时间】:2019-08-27 11:53:41
【问题描述】:
我正在尝试将 top -n1 写入 python 中的文件。它给出了缓冲区大小错误。我是 python 新手,不明白这一点,因为它看起来很简单。
import subprocess
p = subprocess.Popen("top", "n1",stdout=subprocess.PIPE, shell=False)
(output, err) = p.communicate()
topfile = open("top.txt","w+")
topFile.write(output)
topFile.close()
错误:
p = subprocess.Popen("top", "n1",stdout=subprocess.PIPE, shell=False)
文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", 第 659 行,在 init 中 raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
将 buffsize 添加为 int
import subprocess
p = subprocess.Popen("top", "n1",stdout=subprocess.PIPE, shell=False, bufsize =1)
(output, err) = p.communicate()
topHeavyFile = open("topHeavy.txt","w+")
topHeavyFile.write(output)
topHeavyFile.close()
错误:
p = subprocess.Popen("top", "n1",stdout=subprocess.PIPE, shell=False, bufsize =1) TypeError: init() got multiple values for keyword 参数“bufsize”
【问题讨论】:
-
您需要传递
subprocess.Popen(["top", "n1"], stdout .. )而不仅仅是("top", "n1" ..。 -
它现在给了我一个顶级错误,所以这更好。 top -n1 是一个有效的 top 命令,但它会抛出一个 top 错误。我猜我需要推送一组参数,但是 top -n1 如何从 CLI 工作,但在这种情况下它会引发一个 top 错误。如果我将“-n1”推入数组,它就会挂起。
-
我刚做了,它挂了,这很奇怪。我想知道p.communicate是否有问题
标签: python subprocess