最近想把一些查找配置的命令整合在一起,通过运行一个脚本,将计算机的配置信息输出到指定文本,这个过程也遇到了一些困难,毕竟之前没有编程经验,把遇到的问题做个记录如下:
(另外通过运行脚本时利用重定向,也可将结果输出到文件,如 : python test.py > 1.txt )
先粘贴一段小程序,再解释;
21 if __name__ == "__main__":
22 main() 23
以上只实现了一个命令,还要实现其他的,就需要再增加函数调用
现在增加几个函数如下:
#/usr/bin/python
2 #filename: test.py
3
4
5 import subprocess
6 #command 1
7 def func1():
8 uname = "uname"
9 uname_arg = "-a"
10 print " system information is :\n"
11 subprocess.call([uname, uname_arg],stdout=fp)
12
13 #command 2
14 def func2():
15 print "diskspace information is : \n"
16 diskspace = "df"
17 diskspace_arg = "-h"
18 subprocess.call([diskspace,diskspace_arg],stdout=fp)
19
20 #command 3
21 def func3():
22 a = "cat"
23 b = "/proc/cmdline"
24 print "the BIOS and EC information are: \n"
25 subprocess.call([a, b],stdout=fp)
26
27 #command 4
28 def func4():
29 a = "cat"
30 b = "/proc/meminfo"
31 print "the memory information is:\n"
32 subprocess.call([a, b],stdout=fp)
33
34 #command 5
35 def func5():
36 a = "lspci"
37 b = "/proc/cmdline"
38 print "the PCI information are: \n"
39 subprocess.call([a],stdout=fp)
40
41 import sys
42 sys.stdout = open("record.txt","w")
43 fp = open("record.txt","w") 放在这里会导致一个问题,见下面解释;
44 def main(): 更不能放在main函数下面,不然会在其它定义函数中报错,NameError: global name 'fp' is not defined;
45 func1()
46 func2()
47 func3()
48 func4()
49 func5()
50 if __name__ == "__main__":
51 main()
导致的问题是:输出先print的,再是命令调用的,如下:
1 system information is :
2
3 diskspace information is :
4
5 the BIOS and EC information are:
6
7 the memory information is:
8
9 the PCI information are:
10
11 d on
12 /dev/sda5 19G 6.2G 12G 35% /
13 none 997M 212K 996M 1% /dev
14 none 1002M 24K 1002M 1% /dev/shm
15 none 1002M 76K 1002M 1% /var/run
16 none 1002M 0 1002M 0% /var/lock
17 /dev/sda1 118M 44M 68M 40% /boot
18 /dev/sda7 98G 1.5G 92G 2% /home
19 BOOT_IMAGE=/vmlinuz-2.6.35-31-generic root=UUID=5c490b00-f668-42ff-a270-332ac297ea9d ro quiet splash
20 MemTotal: 2051388 kB
21 MemFree: 876036 kB
而且11行前的内容有缺少,原因是print的输出把它覆盖了!!
本来想看能不能放在43或44下方,结果都不行,只有把43行放到每个子函数中去了;
此法仍不能解决上述问题!!
最后只有通过不使用print函数给出提示,来解决覆盖、提示位置不对应的问题;
有机会还需要研究下print与各函数调用之间的先后执行优先级关系情况;
下方附执行结果和代码:
Linux test-desktop 2.6.35-31-generic #63-Ubuntu SMP Mon Nov 28 19:23:11 UTC 2011 i686 G NU/Linux
2 Filesystem Size Used Avail Use% Mounted on
3 /dev/sda5 19G 6.2G 12G 35% /
4 none 997M 212K 996M 1% /dev
5 none 1002M 24K 1002M 1% /dev/shm
6 none 1002M 76K 1002M 1% /var/run
7 none 1002M 0 1002M 0% /var/lock
8 /dev/sda1 118M 44M 68M 40% /boot
9 /dev/sda7 98G 1.5G 92G 2% /home
10 BOOT_IMAGE=/vmlinuz-2.6.35-31-generic root=UUID=5c490b00-f668-42ff-a270-332ac297ea9d ro quiet splash
11 MemTotal: 2051388 kB
12 MemFree: 873988 kB
13 Buffers: 370556 kB
14 Cached: 464560 kB
15 SwapCached: 0 kB
16 Active: 731872 kB
17 Inactive: 264144 kB
18 Active(anon): 161216 kB
19 Inactive(anon): 45512 kB
20 Active(file): 570656 kB
21 Inactive(file): 218632 kB
22 Unevictable: 0 kB
23 Mlocked: 0 kB
24 HighTotal: 1178312 kB
25 HighFree: 543308 kB
26 LowTotal: 873076 kB
27 LowFree: 330680 kB
28 SwapTotal: 1998844 kB
29 SwapFree: 1998844 kB
30 Dirty: 196 kB
31 Writeback: 0 kB....
...................................................省略
代码:
#/usr/bin/python
2 #filename: test.py
3
4
5 import subprocess
6 #command 1
7
8 def func1():
9 uname = "uname"
10 uname_arg = "-a"
11 subprocess.call([uname, uname_arg],stdout=fp)
12
13 #command 2
14 def func2():
15 diskspace = "df"
16 diskspace_arg = "-h"
17 subprocess.call([diskspace,diskspace_arg],stdout=fp)
18
19 #command 3
20 def func3():
21 a = "cat"
22 b = "/proc/cmdline"
23 subprocess.call([a, b],stdout=fp)
24 #command 4
25 def func4():
26 a = "cat"
27 b = "/proc/meminfo"
28 subprocess.call([a, b],stdout=fp)
29
30 #command 5
31 def func5():
32 a = "lspci"
33 b = "/proc/cmdline"
34 subprocess.call([a],stdout=fp)
35
36 import sys
37 sys.stdout = open("record.txt","w")
38 fp = open("record.txt","w")
39 def main():
40 func1()
41 func2()
42 func3() 此处添加print “\n”,也不能起到隔离每个process.call执行结果的作用
43 func4()
44 func5()
45 if __name__ == "__main__":
46 main()
47