笔者使用的操作系统为
windows 7
,python解释器版为python3.7
,pycharm版本为pycharm2018.3
如无特殊说明,代码均在此环境执行
目录
本文主要介绍python执行系统命令的几种方式
一、os.system()
这种方式虽然可以在控制台看到回显的结果,但是却无法接收到这些内容,更无法对结果进行处理
官方文档
对返回结果说明如下
On Windows, the return value is that returned by the system shell
after running command. …it is usually cmd.exe, which returns the
exit status of the command run;
也就是说os.system()返回值是命令执行后退出的状态,正常为0,异常为1
正常情况
In[5]: os.system("dir")
Volume in drive F is 数据
Volume Serial Number is 0006-F904
Directory of F:\Practice\PycharmProjects\PythonBasic
2019/04/29 10:17 <DIR> .
2019/04/29 10:17 <DIR> ..
2019/04/29 11:45 <DIR> .idea
2019/03/31 21:36 <DIR