笔者使用的操作系统为
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

本文详细介绍了Python中执行系统命令的四种方式:os.system()、os.popen()、commands.getstatusoutput()和subprocess.Popen(),包括各自的特点、用法及注意事项。重点讲解了如何获取命令行输出结果,并通过实践案例展示了如何获取本机IP和MAC地址。同时,文中提到了避免管道缓存满导致的死锁问题和处理大量输出数据的方法。
3650

被折叠的 条评论
为什么被折叠?



