python-3.x - subprocess.run().stdout.read() 返回 CompletedProcess 不是字符串
问题描述
我subprocess
用来获取命令行bitwarden
工具的输出以将其重定向到albert
(Linux 的启动器)。我在用着:
returned_user = subprocess.run(["bw", "get", "username", query, "--raw", "--session", session_key], text=True, stdout=subprocess.PIPE, shell=True, check=True).stdout.read()
检查type(returned_user)
给出CompletedProcess
。我如何获得stdout
字符串?subprocess.check_output
也返回 a CompletedProcess
。
一切都在 Python 3.9.1 中完成。
解决方案
参考https://docs.python.org/3/library/subprocess.html,您在参数中错过了 capture_output=True 。我使用 python 3.7.3(在我的 beagelbone 上):
只需尝试 python shell:
>>> import subprocess
>>> command = "echo 123" # your command here.
>>> result = subprocess.run(command, text=True, shell=True, check=True, capture_output=True)
>>> user = 'unknown'
>>> print (user)
unknown
>>> if result.returncode == 0:
... user = result.stdout.strip()
...
>>> print (user)
123
>>>
最好的问候, 伯恩德
推荐阅读
- sql - SQL Server Pivot 作为百分比而不是值并添加总行和列
- scheme - 如何使用 script-fu/scheme 在 gimp 中对图像进行矩形选择
- c++ - 为什么我需要我不直接从我的可执行文件调用的 dll 的导入库?
- c++ - Char 等于 char 不需要的输出
- youtube - 尽管 REL=0,但相关的 youtube 视频显示
- go - 你如何在golang中返回一个非原始类型变量
- c# - c# - 在执行代码时尝试处理任务
- azure - 部署后设备未报告 IoT Edge 上的 Azure 流分析模块
- angular - 如何在Angular2,agm-marker中为自定义标记图标添加边框?
- azure - Azure 容器注册表 + MSI(在服务结构中)- 可能吗?