os-shell-example.py

# -*- coding: utf-8 -*-

# @Date    : 2018-09-30
# @Author  : Peng Shiyu

# 使用os 执行操作系统命令

# system 函数在当前进程下执行一个新命令, 并等待它完成

import os

if os.name == "nt":
    command = "dir"
else:
    command = "ls -l"

# os.system(command)

# 使用os 模块启动新进程
"""
execl
execle
execlp
execlpe
execvp
execvpe

l 接受多个独立的参数(参数列表,类命令行输入)
v 接受一个list或tuple的参数
p 使用PATH环境变量
e 指定的dict环境变量
"""
# os.execvp("python", ("hello.py",))
# 当前进程不会结束

# 使用os 模块调用其他程序(Unix)
# fork 函数复制当前进程, 子进程返回中返回0 在进程中返回子进程的PID
# wait 函数会等待一个子进程执行结束

pid = os.fork()
print(pid)
print("balabala...")

# Windows 上使用spawn函数