看起来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。
我在上面做错了什么?
发布于 2021-04-20 08:39:35
在使用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)https://stackoverflow.com/questions/67169939
复制相似问题