例1:在python中包裝ls命令
#
!/usr/bin/env python
# python wapper for the ls command
import subprocess
subprocess.call([ " ls ", " -l "])
# 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
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
# 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])
# 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] #
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] #