Py&禅

博客园 首页 新随笔 联系 订阅 管理

4.进程的相关处理

system( )给当前进程输入系统shell命令

import os
if os.name == "nt":
      command = "dir"
else:
      command = "ls -l"
      os.system(command)

execvp 开始一个新进程, 以取代目前进程

import os
import sys
program = "python"
arguments = ["hello.py"]
print os.execvp(program, (program,) + tuple(arguments))
print "goodbye"

在windows 平台下用spawn( )等函数执行新进程

import os
import string
def run(program, *args):
# find executable
for path in string.split(os.environ["PATH"], os.pathsep):
       file = os.path.join(path, program) + ".exe"
try:
       return os.spawnv(os.P_WAIT, file, (file,) + args)
except os.error:
       pass
raise os.error, "cannot find executable"
run("python", "hello.py")
print "goodbye"

posted on 2010-05-08 23:18  Py&禅  阅读(3032)  评论(0)    收藏  举报