[英]How to write top output to a file in python?
我正在嘗試將 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整數
將 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 () 為關鍵字參數 'bufsize' 得到多個值
嘗試這個。
import subprocess
p = subprocess.Popen(["top", "n1", "b"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
代表批處理模式的"b"
解決了top: failed tty get
錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.