run()一般在那种需要单纯的返回执行结果的情况下使用,还有很多像“python”这种交互式命令,如果需要输入点什么,然后返回它的执行结果的这种情况,虽然可以用用run()方法的stdin参数接受一个文件句柄的方法来调用系统命令,可是这种方法很不方便,这个时候可以用Popen类来解决,见下一节
其实,run()方法在底层调用的就是Popen接口。
import subprocess
# runcmd = subprocess.run('dir c:\\', encoding='utf-8', shell=True)
# print(runcmd)
def run_cmd(command):
return_cmd = subprocess.run(command, stdout=subprocess.PIPE, stderr