Python OS模块学习(二)
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"
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号