xiaoqiangink

1.使用os.system()去调用,但是只能返回执行状态,不能获取shell cmd执行结果

#!/usr/bin/python
# -*- coding: utf-8

import os
status = os.system("ps aux |grep Xcode |grep -v grep")
print status

2.使用os.popen执行并获取结果

​ 如果返回是str,直接通过read拿结果使用,如果是多行,选择readlines转list获取每行内容

#整份字符串处理
p=os.popen(\'ps aux |grep Xcode |grep -v grep\') 
res=p.read()
print res,type(res)
p.close()

#多行处理
p=os.popen(\'ps aux |grep Xcode |grep -v grep\') 
res1=p.readlines()
for line in res1:
    print \'line :\'+line
p.close()

3.使用commands 模块commands.getstatusoutput()

​ 如果返回是str,直接拿结果使用,如果是多行,选择用splitline转list获取

import commands

status, output = commands.getstatusoutput(\'ps aux |grep Xcode |grep -v grep\')
print output

output_list = output.splitlines()
print output_list

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-03-04
  • 2022-12-23
  • 2021-12-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2021-12-07
  • 2021-06-03
  • 2022-01-13
  • 2022-12-23
相关资源
相似解决方案