写一个使用subprocess的sPopen函数执行shell命令的例子

前端之家收集整理的这篇文章主要介绍了写一个使用subprocess的sPopen函数执行shell命令的例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

给个以前写的例子

#! /usr/bin/env python
# class subprocess test of python language
# 2015-01-07

import subprocess

def popen_test():
    # use class subprocess to get child's pid,# return value,stdin,stdout,stderr etc.
    sPopen = subprocess.Popen("ls ${HOME}",shell=True,stdout=subprocess.PIPE)
    sPopen.wait() # wait for sPopen's process end 
    
    print("sPopen.pid: ",sPopen.pid)
    print("sPopen.returncode: ",sPopen.returncode)
    print("sPopen.stdin: ",sPopen.stdin)
    print("sPopen.stdout.read(): ",sPopen.stdout.read())
    print("sPopen.stderr: ",sPopen.stderr)
    
    # if ... else statement
    if sPopen.pid == sPopen.pid:
    	print("sPopen.pid == sPopen.pid")
    else:
    	print("sPopen.pid != sPopen.pid")
    	
    # communicate can get stderr and stdout
    out = sPopen.communicate()
    print("sPopen.communicate out: ",out)
    print("sPopen.communicate out[0] = ",out[0]," out[1]",out[1],"\n")

    res = subprocess.Popen("echo \"hello python\"",stdout=subprocess.PIPE)
    print("res.stdout.read(): ",res.stdout.read())
    print("res.returncode: ",res.returncode)

def call_test():
    # get return code of shell commands
    retcode = subprocess.call("ls ~/",stdout=subprocess.PIPE)
    print("subprocess.call retcode: ",retcode)
    try:
    	res = subprocess.check_call(['ls','-l'])
    except err:
    	print("subprocess.check_call res: ",res)

if __name__ == "__main__":
    popen_test()
    call_test()
原文链接:https://www.f2er.com/bash/388355.html

猜你在找的Bash相关文章