如果需要在shell中执行命令,可以使用 shell=True 参数
import subprocess
proc = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
print(proc.stdout.read().decode())
import subprocess
result = subprocess.run('ls -l', shell=True, stdout=subprocess.PIPE)
print(result.stdout.decode())
请注意,在 Python 中执行 shell 命令可能带来安全风险,因此应该谨慎使用。