python 执行shell 命令
python 怎么执行shell 命令,并返回shell 执行结果,看下面代码案例
我们shell 命令行运行 ls -l 是列出当前文件夹所有文件信息,下面代码通过 python 执行shell 命令
import subprocess
shell_command = "ls -l"
result = subprocess.run(shell_command, shell=True, capture_output=True)
print(result.stdout.decode('utf-8'))
返回结果和 ls -l 命令一致。
以上就是 python 执行shell 命令