RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
python如何调用shell命令

在Python中,可以使用subprocess模块来调用Shell命令,以下是详细的步骤和示例代码:

1、导入subprocess模块

import subprocess

2、使用subprocess.run()函数执行Shell命令

result = subprocess.run(['ls', 'l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

3、获取命令执行结果

output = result.stdout.decode('utf8')
error = result.stderr.decode('utf8')
return_code = result.returncode

4、打印执行结果

print("输出:", output)
print("错误:", error)
print("返回码:", return_code)

完整示例代码:

import subprocess
result = subprocess.run(['ls', 'l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = result.stdout.decode('utf8')
error = result.stderr.decode('utf8')
return_code = result.returncode
print("输出:", output)
print("错误:", error)
print("返回码:", return_code)

网站栏目:python如何调用shell命令
本文URL:http://jxjierui.cn/article/dpcodoj.html