python调用linux_用python执行Linux命令

例1:在python中包装ls命令

#!/usr/bin/env python

#python wapper for the ls command

import subprocess

subprocess.call(["ls","-l"])

在Linux中执行该命令

[root@pydb python]# python ls.py

total 8

-rwxrwxrwx 1 root root 415 Mar 18 11:40 a.py

-rw-r--r-- 1 root root 103 May 4      22:17 ls.py

下面扩展一下

[root@pydb python]# python ls.py

#!/usr/bin/env python

#python wapper for the ls command

import subprocess

subprocess.call(["cat","/python/ls.py"])

以上是执行Linux下的命令和参数。非常不错的一个例子,好好记住吧

下面一个例子是打印系统信息和磁盘信息的:

#!/usr/bin/env python

#A System Information Gathering Script

import subprocess

#command 1

uname = "uname"

uname_arg = "-a"

print "Gathering system information with %s command:\n" % uname

subprocess.call([uname,uname_arg])

#command 2

diskspace = "df"

diskspace_arg = "-h"

print "Gathering diskspace information %s command:\n" % diskspace

subprocess.call([diskspace,diskspace_arg])

下面是我本地执行打印的数据

[root@pydb python]# python info.py

Gathering system information with uname command:

Linux pydb 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU /Linux

Gathering diskspace information df command:

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

37G 3.9G 31G 11% /

/dev/sda1 99M 13M 82M 13% /boot

tmpfs 506M 0 506M 0% /dev/shm

[root@pydb python]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值