Python运行外部程序的几种方法

使用os.system函数运行其他程序或脚本

import os
os.system('notepad python.txt')

使用ShellExecute函数运行其他程序

ShellExecute(hwnd,op,file,params,dir,bShow)
- hwnd:父窗口的句柄,若没有则为0
- op:要进行的操作,为open,print or 空
- file:要运行的程序或脚本
- params: 要向程序传递的参数,如果打开的是文件则为空
- dir:程序初始化的目录
- bShow:是否显示窗口

ShellExecute(0, 'open', 'notepad.exe', 'python.txt', '', 1)
ShellExecute(0,'open','http://www.baidu.com','','',1)
ShellExecute(0,'open','F:\\Love\\Lady Antebellum - Need You Now.ape','','',1)
ShellExecute(0,'open','D:\Python\Code\Crawler\HanhanBlog.py','','',1)

使用CreateProcess函数

import win32process
from win32process import CreateProcess
CreateProcess('c:\\windows\\notepad.exe', '', None, None, 0,
              win32process.CREATE_NO_WINDOW, None, None,
              win32process.STARTUPINFO())

猜你喜欢

转载自blog.csdn.net/xiligey1/article/details/80267983