python调用shell用什么类_python脚本中调用shell命令
在python腳本中調用類uninx上的系統命令或工具。有下面兩種方法
os.system(string cmd | scriptname.sh)
參數可以是shell命令,也可以是shell腳本。
可用于在遠程類unix上執行shell 命令,如在android底層命令行上執行。該操作將輸出打印到標準輸出中,這也意味著無法用變量去接收命令執行結果,只返回腳本退出時的狀態碼,如
>>>a=os.system("uname -a")
Linuxlocalhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015x86_64 x86_64 x86_64 GNU/Linux
>>>a
0
os.popen(string cmd | scriptname.sh)
返回一個文件對象,要得到命令的輸出內容,只需再調用下read()或readlines()方法。
[NSOS1.1 ~]# python
Python2.4.3 (#1, Dec 10 2010, 17:24:35)
[GCC4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help","copyright", "credits" or "license" for moreinformation.
>>> import os
>>> b=os.popen("cat/usr/local/liveupdate/version").readlines(); #按行讀取,返回列表
>>> b
['#Local version information,SHOULD NOT be modified.\n', 'Product_name=Traffic Control\n','Current_Version=5.0\n', 'Last_Patch_SN=1\n', 'DB_Patch_SN=0\n','Last_Build_SN=0\n', 'Last_Hotfix_SN=0\n', 'Type=tc\n']
>>> b[1]
'Product_name=Traffic Control\n'
>>>type(b)
>>>
總結
以上是生活随笔為你收集整理的python调用shell用什么类_python脚本中调用shell命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pcm 8k单声道和双声道采样_音频接口
- 下一篇: python unicodedecode