简体   繁体   English

当命令包含连字符时,如何使用 Python subprocess.run?

[英]How to use Python subprocess.run when the command contains a hyphen?

I'm trying to run the following command using subprocess.run , but I think the hyphen in the command ir-keytable , is confusing the parser.我正在尝试使用subprocess.run运行以下命令,但我认为命令ir-keytable中的连字符会混淆解析器。 What's the best way to solve this?解决这个问题的最佳方法是什么?

ir-keytable -t -s rc0

The error:错误:

stdout: 
stderr: Traceback (most recent call last):
 File "<string>", line 1, in <module>
NameError: name 'ir' is not defined

My code:我的代码:

import subprocess
import sys

result = subprocess.run(
    [sys.executable, "-c", "ir-keytable('-t -s rc0')"], capture_output=True, text=True
)
print("stdout:", result.stdout)
print("stderr:", result.stderr)

Your Syntax is giving error for any command, you should just try this:您的语法对任何命令都给出错误,您应该试试这个:

import subprocess

# If you just want to run command then this :
result=subprocess.run("<>")

# But if you want to get result of your command the this:
result=subprocess.check_output("<>")
print(result.decode("utf-8")) # It returns byte type data, so we are converting that into utf-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python中如何使用subprocess.run方法? - How to use subprocess.run method in python? 如何使用 subprocess.run() 在 Python 中运行 cmd 命令 - How to run a cmd command in Python using subprocess.run() python subprocess.run() 错误地解析命令 - python subprocess.run() wrongly parses command Python 3:如何以管理员身份使用 subprocess.run() (Windows 10) - Python 3: How to use subprocess.run() as admin (windows 10) 当我在异常中使用python中的subprocess.run()时,如何知道错误是什么? - How to know what the error is when I use subprocess.run() in python with exception? 如何使用subprocess.run启动python shell - How to launch a python shell with subprocess.run 如何在 python 中将变量解析为 subprocess.run - How to parse a variables to subprocess.run in python 当arg已经包含“”和“”引号时,如何将字符串arg提交给subprocess.run(arg,shell = True)? - How to submit a string arg to subprocess.run(arg, shell=True) when arg already contains “ ” and ' ' quotes? 从 /usr/bin/* 执行命令时来自 subprocess.run 的 FileNotFoundError - Python - FileNotFoundError from subprocess.run when executing command from /usr/bin/* - Python 如何在 Python 中使用 subprocess.run 运行包含引号的 tsduck shell 命令 - How to run this tsduck shell command containing quotes with subprocess.run in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM