python subprocess

本文详细介绍了Python中subprocess模块的使用方法,包括如何执行简单命令、处理复杂命令、使用Popen类及其方法,并展示了如何通过stdin、stdout和stderr与子进程交互。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-12-12 15:02:43
# @Author  : eddy (278298125@qq.com)
# @Link    : http://my.oschina.net/eddylinux
# @Version : 1.0

import os
import commands
import subprocess
# os.system('ls /')
# os.popen('ls /')
# result = commands.getoutput('cmd')
# result = commands.getstatus('cmd')
# result = commands.getstatusoutput('cmd')
# call 
# 执行命令,返回状态码
ret = subprocess.call(["ls", "-l"], shell=False)

# check_call
# 执行命令,如果执行状态码是 0 ,则返回0,否则抛异常
subprocess.check_call(["ls", "-l"])
subprocess.check_call("exit 1", shell=True)

# subprocess.Popen(...)
# 用于执行复杂的系统命令
# 参数
# args:shell命令,可以是字符串或者序列类型(如:list,元组)
# bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
# stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
# preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
# close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
# 所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
# shell:同上
# cwd:用于设置子进程的当前目录
# env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
# universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
# startupinfo与createionflags只在windows下有效
# 将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
ret1 = subprocess.Popen(["ls","l","/"])
ret2 = subprocess.Popen(["ls","l","/"], shell=True)

# 终端输入的命令分为两种:

# 输入即可得到输出,如:ifconfig
# 输入进行某环境,依赖再输入,如:python
obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write('print 1 \n ')
obj.stdin.write('print 2 \n ')
obj.stdin.write('print 3 \n ')
obj.stdin.write('print 4 \n ')
obj.stdin.close()

cmd_out = obj.stdout.read()
obj.stdout.close()
cmd_error = obj.stderr.read()
obj.stderr.close()

print cmd_out
print cmd_error

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write('print 1 \n ')
obj.stdin.write('print 2 \n ')
obj.stdin.write('print 3 \n ')
obj.stdin.write('print 4 \n ')

out_error_list = obj.communicate()
print out_error_list

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_error_list = obj.communicate('print "hello"')
print out_error_list

a = subprocess.Popen(['ls','.'],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print a.stdout.read()
print type(a.stdout.read())
a.stdout.close()

b = subprocess.Popen(['ls','.'],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
c =  b.communicate()
print c
print type(c)
# check_db1.sh
# check_db2.sh
# command1.py
# conn_mysql.py
# disk.py
# monitor.py
# rename1.sh
# rename.sh
# temp
# test1.sh
# test3.sh
# test.sh
# <type 'str'>

# ('check_db1.sh\ncheck_db2.sh\ncommand1.py\nconn_mysql.py\ndisk.py\nmonitor.py\nrename1.sh\nrename.sh\ntemp\ntest1.sh\ntest3.sh\ntest.sh\n', '')
# <type 'tuple'>


转载于:https://my.oschina.net/eddylinux/blog/543189

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值