zoukankan      html  css  js  c++  java
  • appium自动化测试框架——在python脚本中执行dos命令

    一般我们运行dos命令,会有两种需求,一种是需要收集执行结果,如ip、device等;一种是不需要收集结果,如杀死或开启某个服务。

    对应的在python中就要封装两种方法,来分别实现这两种需求。

    1、引入包

    import os

    2、只执行不收集结果

    os.system(command)

    3、执行并手机结果

    os.popen(command).readlines()

    4、代码实现

     1 #coding=utf-8
     2 import os
     3 
     4 class DosCmd:
     5     '''
     6     用来封装windows执行dos命令,分两种,一种是收集执行结果,一种是不需要收集执行结果
     7     '''
     8     def excute_cmd_result(self,command):
     9         '''
    10         执行command命令,并返回执行结果
    11         :param command: 传入要执行的命令,字符串格式
    12         :return:返回执行结果,列表格式
    13         '''
    14         result_list = []
    15         result = os.popen(command).readlines()
    16         for i in result:
    17             if i == '
    ':
    18                 continue
    19             result_list.append(i.strip('
    '))#strip() 方法用于移除字符串头尾指定的字符
    20         return result_list
    21 
    22 
    23     def excute_cmd(self,command):
    24         '''
    25         仅执行command命令,不收集执行结果
    26         :param command: 传入要执行的命令,字符串格式
    27         '''
    28         os.system(command)
    29 
    30 if __name__=="__main__":
    31     dos = DosCmd()
    32     print dos.excute_cmd_result('adb devices')
    33     dos.excute_cmd('adb devices')
  • 相关阅读:
    lambda表达式
    You can't specify target table 't_mail_marketing' for update in FROM clause
    从对象list中获取对象属性list
    枚举缓存
    Solr语法
    通过Telnet查询注册服务
    日志查询
    Maven 屏蔽静态文件
    抽奖
    随机码生成方法
  • 原文地址:https://www.cnblogs.com/loveapple/p/9141519.html
Copyright © 2011-2022 走看看