软件测试学习 之 Python 获取CMD命令行输出结果

笔者使用的操作系统为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
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值