• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
金大鑫要坚持
博客园    首页    新随笔    联系   管理    订阅  订阅

【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.

 

posted @ 2016-10-19 16:55  金大鑫要坚持  阅读(383)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3