【Python】 Subprocess module

1. subprocess.check_output()

2.subprocess.call()

3. subprocess.check_call()

the methods 1.2.3 are are wrapper of subprocess.Popen()

  • example 1:

Open Visual Studio through python

This way is incorrect.

>>> retcode = subprocess.call('devenv.exe', shell=True)
'devenv.exe' is not recognized as an internal or external command,
operable program or batch file.

Another way:

retcode = subprocess.call("C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe",shell=True)

  It's done.

Your VS is opened. But the invoke process is blocked. You have to close the process you spawned to get the return code.

  • example 2:

try to run iisreset command with python

>>> print subprocess.check_call("iisreset", shell = True)

Attempting stop...
Internet services successfully stopped
Attempting start...
Internet services successfully restarted
0

 class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

What the Popen returns is a child process you want to create.

example 3:

the Popen() process will not be blocked unless we explicit child.wait()

import subprocess
def havewait():
    child = subprocess.Popen(['ping','vwijin014'])
    child.wait()
    print 'parent process'

def nowait():
    child = subprocess.Popen(['ping','vwijin014'])
    #child.wait()
    print 'parent process'

Pay attention to the red line order. One is before the ping running.

 

转载于:https://www.cnblogs.com/jin-wen-xin/p/5977392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值