1. 程式人生 > >python模組commands執行系統命令

python模組commands執行系統命令

    上一篇中提到了執行系統命令的新利器os模組,其中有兩個方法可以用來執行系統命令一個是os.popen()一個是os.system(),還有兩種可以用來執行系統命令的方法一個是:python自帶的commands模組另一個是python的程序管理模組subprocess。這裡主要說的是python自帶的commands模組。

    看一個簡單的演示,依舊使用的是之前的排序演算法:

>>> (status, commands)=commands.getstatusoutput('python dui.py')
>>> print (status, commands)
(0, '\xe9\x9a\x8f\xe6\x9c\xba\xe5\xba\x8f\xe5\x88\x97\xe7\x94\x9f\xe6\x88\x90\xe8\x80\x97\xe6\x97\xb6\xef\xbc\x9a 0.000231981277466\n\xe5\xa0\x86\xe6\x8e\x92\xe5\xba\x8f\xe7\xbb\x93\xe6\x9e\x9c\xe4\xb8\xba\n[6, 15, 42, 57, 68, 78, 86, 88, 128, 130, 131, 141, 152, 155, 163, 165, 166, 178, 199, 220, 229, 244, 248, 248, 251, 262, 262, 280, 291, 319, 325, 340, 344, 347, 356, 393, 398, 409, 421, 421, 423, 448, 450, 457, 459, 469, 475, 488, 489, 507, 519, 534, 534, 540, 542, 544, 547, 548, 586, 607, 610, 630, 640, 646, 666, 667, 673, 693, 702, 708, 730, 731, 746, 758, 777, 794, 802, 803, 804, 804, 808, 828, 863, 869, 874, 878, 881, 898, 902, 919, 920, 923, 936, 943, 950, 951, 971, 973, 980, 996]\n\xe5\xa0\x86\xe6\x8e\x92\xe5\xba\x8f\xe6\x80\xbb\xe8\x80\x97\xe6\x97\xb6\xef\xbc\x9a 0.000458955764771\n\xe5\xbd\x92\xe5\xb9\xb6\xe6\x8e\x92\xe5\xba\x8f\xe7\xbb\x93\xe6\x9e\x9c\xe4\xb8\xba\n[6, 15, 42, 57, 68, 78, 86, 88, 128, 130, 131, 141, 152, 155, 163, 165, 166, 178, 199, 220, 229, 244, 248, 248, 251, 262, 262, 280, 291, 319, 325, 340, 344, 347, 356, 393, 398, 409, 421, 421, 423, 448, 450, 457, 459, 469, 475, 488, 489, 507, 519, 534, 534, 540, 542, 544, 547, 548, 586, 607, 610, 630, 640, 646, 666, 667, 673, 693, 702, 708, 730, 731, 746, 758, 777, 794, 802, 803, 804, 804, 808, 828, 863, 869, 874, 878, 881, 898, 902, 919, 920, 923, 936, 943, 950, 951, 971, 973, 980, 996]\n\xe5\xbd\x92\xe5\xb9\xb6\xe6\x8e\x92\xe5\xba\x8f\xe6\x80\xbb\xe8\x80\x97\xe6\x97\xb6\xef\xbc\x9a 0.00229287147522\n\xe5\xbf\xab\xe9\x80\x9f\xe6\x8e\x92\xe5\xba\x8f\xe7\xbb\x93\xe6\x9e\x9c\xe4\xb8\xba\n[6, 15, 42, 57, 68, 78, 86, 88, 128, 130, 131, 141, 152, 155, 163, 165, 166, 178, 199, 220, 229, 244, 248, 248, 251, 262, 262, 280, 291, 319, 325, 340, 344, 347, 356, 393, 398, 409, 421, 421, 423, 448, 450, 457, 459, 469, 475, 488, 489, 507, 519, 534, 534, 540, 542, 544, 547, 548, 586, 607, 610, 630, 640, 646, 666, 667, 673, 693, 702, 708, 730, 731, 746, 758, 777, 794, 802, 803, 804, 804, 808, 828, 863, 869, 874, 878, 881, 898, 902, 919, 920, 923, 936, 943, 950, 951, 971, 973, 980, 996]\n\xe5\xbf\xab\xe9\x80\x9f\xe6\x8e\x92\xe5\xba\x8f\xe6\x80\xbb\xe8\x80\x97\xe6\x97\xb6\xef\xbc\x9a 0.00102496147156')

    幾個方法都有異曲同工之妙,跟直接使用Linux指令沒有多大的差別。

    接下來使用上一篇文章中說到的os.system()和commands做一個對比的演示:

功能:輸出Linux.txt前十行內容
>>> print os.system('head Linux.txt')
1.[[email protected]/root]# 表示登陸進去系統,其中#是超級使用者也即root使用者的系統提示符
2.reboot命令可以重啟系統
3.關閉系統使用(該命令只可被超級使用者使用):shutdown[選項][時間][警告資訊]
-k:不真正關機只是發警告系想你給所有使用者
-r:關機後立刻重啟
-h:關機後不啟動
-f:快速關機重啟時跳過fsck
-n:快速關機不經過init程式
-c:取消一個已經執行的shutdown
4.檔案操作命令
0
>>> print commands.getstatusoutput('head Linux.txt')
(0, '1.[
[email protected]
/root]# \xe8\xa1\xa8\xe7\xa4\xba\xe7\x99\xbb\xe9\x99\x86\xe8\xbf\x9b\xe5\x8e\xbb\xe7\xb3\xbb\xe7\xbb\x9f\xef\xbc\x8c\xe5\x85\xb6\xe4\xb8\xad#\xe6\x98\xaf\xe8\xb6\x85\xe7\xba\xa7\xe7\x94\xa8\xe6\x88\xb7\xe4\xb9\x9f\xe5\x8d\xb3root\xe7\x94\xa8\xe6\x88\xb7\xe7\x9a\x84\xe7\xb3\xbb\xe7\xbb\x9f\xe6\x8f\x90\xe7\xa4\xba\xe7\xac\xa6\r\n2.reboot\xe5\x91\xbd\xe4\xbb\xa4\xe5\x8f\xaf\xe4\xbb\xa5\xe9\x87\x8d\xe5\x90\xaf\xe7\xb3\xbb\xe7\xbb\x9f\r\n3.\xe5\x85\xb3\xe9\x97\xad\xe7\xb3\xbb\xe7\xbb\x9f\xe4\xbd\xbf\xe7\x94\xa8\xef\xbc\x88\xe8\xaf\xa5\xe5\x91\xbd\xe4\xbb\xa4\xe5\x8f\xaa\xe5\x8f\xaf\xe8\xa2\xab\xe8\xb6\x85\xe7\xba\xa7\xe7\x94\xa8\xe6\x88\xb7\xe4\xbd\xbf\xe7\x94\xa8\xef\xbc\x89:shutdown[\xe9\x80\x89\xe9\xa1\xb9][\xe6\x97\xb6\xe9\x97\xb4][\xe8\xad\xa6\xe5\x91\x8a\xe4\xbf\xa1\xe6\x81\xaf]\r\n-k\xef\xbc\x9a\xe4\xb8\x8d\xe7\x9c\x9f\xe6\xad\xa3\xe5\x85\xb3\xe6\x9c\xba\xe5\x8f\xaa\xe6\x98\xaf\xe5\x8f\x91\xe8\xad\xa6\xe5\x91\x8a\xe7\xb3\xbb\xe6\x83\xb3\xe4\xbd\xa0\xe7\xbb\x99\xe6\x89\x80\xe6\x9c\x89\xe7\x94\xa8\xe6\x88\xb7\r\n-r\xef\xbc\x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