1、概述
2、os包
3、subprocess包
4、对比
5、参考文章python
python与shell交互的方式有一下几种:os.system()
os.popen()
commands包
subprocess包
shell
import os #使用读通道 f = os.popen("ls -l")# 执行此语句后,将建立子shell运行参数中指定的命令,可是不阻塞父进程 f.read()# 执行读取操做时,将阻塞父进程 #使用写管道 #须要提早写好echo文件 ''' #echo.py while True: in = input() if in == "quit" or in == "exit": print("Bye") break print("[-] ", in) ''' f = os.popen("python echo.py", "w") f.write("hello")
os.system
、os.spawn*
等call(*popenargs, timeout= None, **kwargs)
ide
os.system()
check_call(*popenargs, **kwargs)
函数
CalledProcessError
异常)check_output(*popenargs, timeout=None, **kwargs)
CalledProcessError
异常)input
getoutput(cmd)
ui
getstatusoutput(cmd)
spa
run(args, *, stdin = None, input = None, stderr = None, shell = False, cwd = None, timeout = None, check = False, encoding = None, errors = None)
run
函数是对Popen
类封装后造成的简单函数