python - Python子进程将双引号附加到最后一个arg
问题描述
似乎 python subprocess.run 在最后一个参数后附加了一个双引号:
Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> args = ['cmd', '/c', 'echo', 'hello']
>>> result = subprocess.run(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> result.stdout
b'hello"\r\n'
>>> stdout = str(result.stdout, "utf-8").strip()
>>> stdout
'hello"'
我正在使用 Windows 20H2 19042.928。
我在上面做错了什么?
解决方案
使用 subprocess.run() 时,您的 args 已经在默认 cmd 或终端(取决于您的操作系统)中运行。所以,你不需要 cmd arg。您只需要;
import subprocess
args = ['echo', 'hello']
result = subprocess.run(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = str(result.stdout, 'utf-8').strip()
print(out)
推荐阅读
- python - 屏幕分离后如何通过 SSH 正确重新连接
- java - 使用 EntryProcessor 时让 Hazelcast 停止反序列化对象
- javascript - 使用 JavaScript 到 HTML 对所有可见文本执行功能?
- ssl - 从向 tomcat 服务器发出请求的客户端(浏览器)获取 TLS 版本
- python - 使用 Google BigQuery Python 库创建或替换表
- c# - 即使在 CRM 动态中使用了正确的凭据,也无法连接到组织服务 - C#
- c - 为什么我在访问整数数组时会立即出现段错误?
- git - 在 repo 历史记录中一直传播对 txt 文件的编辑
- angular - Angular 中的网络工作者
- php - 致命错误:未捕获的错误:在第 73 行调用未定义函数 add_category()。如何解决此问题?