国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python調用shell cmd方法代碼示例解析

瀏覽:4日期:2022-07-20 16:58:04

1.使用os.system()去調用,但是只能返回執行狀態,不能獲取shell cmd執行結果

#!/usr/bin/python# -*- coding: utf-8import osstatus = 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 :’+linep.close()

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

​ 如果返回是str,直接拿結果使用,如果是多行,選擇用splitline轉list獲取

import commandsstatus, output = commands.getstatusoutput(’ps aux |grep Xcode |grep -v grep’)print outputoutput_list = output.splitlines()print output_list

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 县级市| 四平市| 万山特区| 蕉岭县| 余江县| 鹤岗市| 肇源县| 青岛市| 内黄县| 福建省| 舒兰市| 蒙城县| 手游| 巴青县| 枣阳市| 高要市| 正定县| 大田县| 临澧县| 瑞金市| 上杭县| 泾阳县| 吉木乃县| 康保县| 鄂尔多斯市| 万载县| 怀化市| 托里县| 武山县| 呼玛县| 西贡区| 当雄县| 进贤县| 临高县| 三门峡市| 孝昌县| 阿拉尔市| 满城县| 北流市| 肥西县| 彝良县|