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

发布时间:2019-08-17 09:07:41编辑:auto阅读(1980)

    使用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())

关键字