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.[root@localhost/root]# 表示登陆进去系统,其中#是超级用户也即root用户的系统提示符
2.reboot命令可以重启系统
3.关闭系统使用(该命令只可被超级用户使用):shutdown[选项][时间][警告信息]
-k:不真正关机只是发警告系想你给所有用户
-r:关机后立刻重启
-h:关机后不启动
-f:快速关机重启时跳过fsck
-n:快速关机不经过init程序
-c:取消一个已经运行的shutdown
4.文件操作命令
0
>>> print commands.getstatusoutput('head Linux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\x8b\xe5\x88\xbb\xe9\x87\x8d\xe5\x90\xaf\r\n-h:\xe5\x85\xb3\xe6\x9c\xba\xe5\x90\x8e\xe4\xb8\x8d\xe5\x90\xaf\xe5\x8a\xa8\r\n-f:\xe5\xbf\xab\xe9\x80\x9f\xe5\x85\xb3\xe6\x9c\xba\xe9\x87\x8d\xe5\x90\xaf\xe6\x97\xb6\xe8\xb7\xb3\xe8\xbf\x87fsck\r\n-n:\xe5\xbf\xab\xe9\x80\x9f\xe5\x85\xb3\xe6\x9c\xba\xe4\xb8\x8d\xe7\xbb\x8f\xe8\xbf\x87init\xe7\xa8\x8b\xe5\xba\x8f\r\n-c:\xe5\x8f\x96\xe6\xb6\x88\xe4\xb8\x80\xe4\xb8\xaa\xe5\xb7\xb2\xe7\xbb\x8f\xe8\xbf\x90\xe8\xa1\x8c\xe7\x9a\x84shutdown\r\n4.\xe6\x96\x87\xe4\xbb\xb6\xe6\x93\x8d\xe4\xbd\x9c\xe5\x91\xbd\xe4\xbb\xa4\r')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Together_CZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值