Python:在一个Python程序中,运行另一个Python程序 |
您所在的位置:网站首页 › 在linux中运行python文件 › Python:在一个Python程序中,运行另一个Python程序 |
学习自: 1~3学习自如何在python中执行另一个py文件_python_脚本之家 4~6学习自Python中四种运行其他程序的方式 - hankleo - 博客园 1、os.system方法用法: os.system('python3 xxx.py')说明: 就相当于在cmd窗口中写python3 xxx.py,即运行Python程序。 支持传递参数 2、execfile('xxx.py') 3、如果想要得到这个文件的输出,可以用os.popen 4、ShellExecute用法: ShellExecute(hwnd, op, file, args, dir, show) hwnd 父窗口的句柄,如果没有父窗口,则为0 op 要运行的操作,open、print、空 file 要运行的程序 args 传递的参数 dir 程序所在目录 show 是否显示窗口例子: import win32api.ShellExecute ShellExecute(0, 'open', 'notepad.exe', '', '', 0) #show=0,后台执行 ShellExecute(0, 'open', 'notepad.exe', '', '', 1) #前台打开 ShellExecute(0, 'open', 'notepad.exe', '1.txt', '', 1)#传入参数1.txt ShellExecute(0, 'open', 'http://www.sohu.com', '', '', 1)#打开网页 ShellExecute(0, 'open', 'D:\\Opera.mp3', '', '', 1)#打开音频文件,当路径指向一个文件时 ShellExecute(0, 'open', 'D:\\hello.py', '', '', 1)#运行程序原理:使用ShellExecute函数,相当于在资源管理器中双击文件图标,系统会打开相应程序进行运行。 5、CreateProcess顾名思义,CreateProcess即创建进程,通过win32process模块中的CreateProcess()函数创建一个运行相应程序的进程。其函数格式为: CreateProcess(appName,cmdLine,proAttr,threadAttr,InheritHandle,CreationFlags,newEnv,currentDir,Attr) appName 可执行文件名 cmdLine 命令行参数 procAttr 进程安全属性 threadAttr 线程安全属性 InheritHandle 继承标志 CreationFlags 创建标志 currentDir 进程的当前目录 Attr 创建程序的属性举例: win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO()) (, , 21592, 18780) # 函数返回进程句柄、线程句柄、进程ID以及线程ID结束进程:win32process.TerminateProcess TerminateProcess(handle,exitCode)handle:要操作的进程句柄 exitCode:进程退出代码 补充:该方法应该只能打开exe文件,打开其他文件时会报错 6、使用ctypes调用kernel32.dll中的函数使用ctypes模块可以让Python调用位于动态链接库的函数。可以方便地调用由C语言编写的动态链接库,并向其传递参数。
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |