Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1669925
  • 博文数量: 636
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3950
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-06 21:58
个人简介

博客是我工作的好帮手,遇到困难就来博客找资料

文章分类

全部博文(636)

文章存档

2024年(5)

2022年(2)

2021年(4)

2020年(40)

2019年(4)

2018年(78)

2017年(213)

2016年(41)

2015年(183)

2014年(66)

我的朋友

分类: 系统运维

2015-04-01 14:52:51

os.system('cat /proc/cpuinfo')


output = os.popen('cat /proc/cpuinfo')


print output.read()


通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是怎么读取程序执行的返回值呢。Google 给我指向了 commands — Utilities for running commands。


这样通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。


(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')


print status, output


Python Document 中给的一个例子,很清楚的给出了各方法的返回。


>>> import commands


>>> commands.getstatusoutput('ls /bin/ls')


(0, '/bin/ls')


>>> commands.getstatusoutput('cat /bin/junk')


(256, 'cat: /bin/junk: No such file or directory')


>>> commands.getstatusoutput('/bin/junk')


(256, 'sh: /bin/junk: not found')


>>> commands.getoutput('ls /bin/ls')


'/bin/ls'
>>> commands.getstatus('/bin/ls')


'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls' 



cat  /home/haoren/study-python/shuju.txt 

170302-10:00:18 Bill[40268] TRACE: [人民币统计][通用人民币操作]用户扣除人民币,用户:56382627,人民币:19009423
170302-10:00:18 Bill[40268] TRACE: [人民币统计][通用人民币操作]用户扣除人民币,用户:56382627,人民币:19009323
170302-10:00:18 Bill[40268] TRACE: [人民币统计][通用人民币操作]用户扣除人民币,用户:53621808,人民币:13913410
170302-10:00:18 Bill[40268] TRACE: [人民币统计][通用人民币操作]用户扣除人民币,用户:57032579,人民币:9094648
170302-10:00:01 Bill[40268]  INFO: [人民币结算]时间(1488420001),用户ID(41077113),原来人民币(59312),现在人民币(59112),人民币操作(2),扣除(200),操作类型(11),操作详情(0),操作数量(0).描述:连连购
170302-10:00:01 Bill[40268]  INFO: [人民币结算]时间(1488420001),用户ID(41077113),原来人民币(59112),现在人民币(59012),人民币操作(2),扣除(100),操作类型(11),操作详情(0),操作数量(0).描述:连连购
170302-10:00:02 Bill[40268]  INFO: [人民币结算]时间(1488420002),用户ID(31869180),原来人民币(6733),现在人民币(6633),人民币操作(2),扣除(100),操作类型(1),操作详情(80),操作数量(1).描述:送礼品
170302-10:00:11 Bill[40268]  INFO: [人民币结算]时间(1488420011),用户ID(70461470),原来人民币(20453),现在人民币(20353),人民币操作(2),扣除(100),操作类型(11),操作详情(0),操作数量(0).描述:连连购
170302-10:00:11 Bill[40268]  INFO: [人民币结算]时间(1488420011),用户ID(70461470),原来人民币(20353),现在人民币(20253),人民币操作(2),扣除(100),操作类型(11),操作详情(0),操作数量(0).描述:连连购
170302-10:00:11 Bill[40268]  INFO: [人民币结算]时间(1488420011),用户ID(53537267),原来人民币(3380),现在人民币(3280),人民币操作(2),扣除(100),操作类型(11),操作详情(0),操作数量(0).描述:连连购

#!/usr/bin/python
#-*- coding:UTF-8 -*-
import calendar
import datetime
import MySQLdb
import os, sys, re,string
import time, tarfile,getopt


import random
import pwd
import spwd

import commands


list = []


dic = {}


os.system("cat /home/haoren/study-python/shuju.txt |grep 41077113 |awk -F '[()]' '{print $2}'")


output = os.popen('cat /home/haoren/study-python/shuju.txt |grep 41077113')

print output.read()


(status, output) = commands.getstatusoutput('cat /home/haoren/study-python/shuju.txt |grep 41077113')


print  status
print  output


执行结果

1488420001
1488420001
170302-10:00:01 Bill[40268]  INFO: [人民币结算]时间(1488420001),用户ID(41077113),原来人民币(59312),现在人民币(59112),人民币操作(2),扣除(200),操作类型(11),操作详情(0),操作数量(0).描述:连连购
170302-10:00:01 Bill[40268]  INFO: [人民币结算]时间(1488420001),用户ID(41077113),原来人民币(59112),现在人民币(59012),人民币操作(2),扣除(100),操作类型(11),操作详情(0),操作数量(0).描述:连连购


0
170302-10:00:01 Bill[40268]  INFO: [人民币结算]时间(1488420001),用户ID(41077113),原来人民币(59312),现在人民币(59112),人民币操作(2),扣除(200),操作类型(11),操作详情(0),操作数量(0).描述:连连购
170302-10:00:01 Bill[40268]  INFO: [人民币结算]时间(1488420001),用户ID(41077113),原来人民币(59112),现在人民币(59012),人民币操作(2),扣除(100),操作类型(11),操作详情(0),操作数量(0).描述:连连购




os.system(cmd)的返回值只会有0(成功),1,2


os.popen(cmd)会把执行的cmd的输出作为值返回。
 
Python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容。实际使用时视需求情况而选择。
现假定有一个shell脚本test.sh:
#!/bin/bash
1. echo "hello world!"
2. exit 3




os.system(cmd):


该方法在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码,即脚本中“exit 1”的代码执行后,os.system函数返回值的高位数则是1,如果低位数是0的情况下,则函数的返回值是0×100,换算为10进制得到256。


如果我们需要获得os.system的正确返回值,那使用位移运算可以还原返回值:
1. >>>  n = os.system(test.sh)
2. >>> n >> 8
3. >>> 3
os.popen(cmd):


这种调用方式是通过管道的方式来实现,函数返回一个file-like的对象,里面的内容是脚本输出的内容(可简单理解为echo输出的内容)。使用os.popen调用test.sh的情况:


python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容。实际使用时视需求情况而选择。


明显地,像调用”ls”这样的shell命令,应该使用popen的方法来获得内容


以前就疑惑popen和system有什么不同,今天实际的应用,才让我恍然大悟


os.popen()可以实现一个“管道”,从这个命令获取的值可以继续被调用。而os.system不同,它只是调用,调用完后自身退出,可能返回个0吧


比如,我想得到ntpd的进程id,就要这么做:


os.popen('ps -C ntpd | grep -v CMD |awk '{ print $1 }').readlines()[0]


 Python中执行系统命令常见方法有两种:


两者均需 import os

(1) os.system

# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息

system(command) -> exit_status
Execute the command (a string) in a subshell.

# 如果再命令行下执行,结果直接打印出来

1 >>> os.system('ls')
2 04101419778.CHM   bash      document    media      py-django   video
3 11.wmv            books     downloads   Pictures  python
4 all-20061022      Desktop   Examples    project    tools

(2) os.popen

# 该方法不但执行命令还返回执行后的信息对象

popen(command [, mode='r' [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.

例如:

01 >>>tmp = os.popen('ls *.py').readlines()
02 >>>tmp
03 Out[21]:
04 ['dump_db_pickle.py ',
05 'dump_db_pickle_recs.py ',
06 'dump_db_shelve.py ',
07 'initdata.py ',
08 '__init__.py ',
09 'make_db_pickle.py ',
10 'make_db_pickle_recs.py ',
11 'make_db_shelve.py ',
12 'peopleinteract_query.py ',
13 'reader.py ',
14 'testargv.py ',
15 'teststreams.py ',
16 'update_db_pickle.py ',
17 'writer.py ']

好处在于:将返回的结果赋于一变量,便于程序的处理。

(3)  使用模块subprocess

1 >>> import subprocess
2 >>> subprocess.call (["cmd", "arg1", "arg2"],shell=True)

获取返回和输出:

1 import subprocess
2 p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
3 for line in p.stdout.readlines():
4     print line,
5 retval = p.wait()

(4)  使用模块commands模块

1 >>> import commands
2 >>> dir(commands)
3 ['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'getoutput', 'getstatus','getstatusoutput', 'mk2arg', 'mkarg']
4 >>> commands.getoutput("date")
5 'Wed Jun 10 19:39:57 CST 2009'
6 >>>
7 >>> commands.getstatusoutput("date")
8 (0, 'Wed Jun 10 19:40:41 CST 2009')

注意: 当执行命令的参数或者返回中包含了中文文字,那么建议使用subprocess,如果使用os.popen则会出现下面的错误:

1 Traceback (most recent call last):
2   File "./test1.py", line 56, in 
3     main()
4   File "./test1.py", line 45, in main
5     fax.sendFax()
6   File "./mailfax/Fax.py", line 13, in sendFax
7     os.popen(cmd)
8 UnicodeEncodeError: 'ascii' codec can't encode characters in position 46-52: ordinal not inrange(128)


阅读(770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~