繁体   English   中英

使用Python用终端运行一个文件

[英]Using Python to run a file with the terminal

我正在寻找一种通过终端运行文件的方法,但我不想输入它,而是想运行一个 python 脚本,该脚本为我通过终端运行文件。

此代码应通过终端运行:

cd LCD-show
sudo ./LCD35-show

我已经尝试过这个并在 Python 中运行它:

import os
os.system("./LCD35-show")

但这不起作用。 我怎么解决这个问题?

试穿这个尺寸:

import subprocess
p1 = subprocess.run("cd LCD-show ; sudo ./LCD35-show", shell=True, text=True, capture_output=True)
print(p1.returncode)
print(p1.stdout)
print(p1.stderr)

shell=True选项通过 shell 运行整个 arg,因此用分号分隔的多个命令起作用。 text=True意味着将stdoutstderr视为字符串,而不是字节。 最后, capture_output=True意味着实际捕获stdoutstderr作为属性。

如果你在myFile.sh中有一个完整的命令文件,那基本上是一样的。 请注意myFile.sh的 exec 权限和任何PATH考虑事项; 你可能需要做./myFile.sh

p1 = subprocess.run("myFile.sh", shell=True, text=True, capture_output=True)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM