[英]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.