Python呼叫(執行)外部程式

2021-06-19 12:15:36 字數 4281 閱讀 9988

在python中可以方便地使用os模組執行其他的指令碼或者程式,這樣就可以在指令碼中直接使用其他指令碼,或者程式提供的功能,而不必再次編寫實現該功能的**。為了更好地控制執行的程序,可以使用win32process模組中的函式。如果想進一步控制程序,則可以使用ctype模組,直接呼叫kernel32.dll中的函式。

1 使用os.system函式執行其他程式

2 使用shellexecute函式執行其他程式

3 使用createprocess函式執行其他程式

4 使用ctypes呼叫kernel32.dll中的函式

1 使用os.system函式執行其他程式

os模組中的system()函式可以方便地執行其他程式或者指令碼。其函式原型如下所示。

os.system(command)

其引數含義如下所示。

command 要執行的命令,相當於在windows的cmd視窗中輸入的命令。如果要向程式或者指令碼傳遞引數,可以使用空格分隔程式及多個引數。

以下例項實現通過os.system()函式開啟系統的記事本程式。

>>> import os

# 使用os.system()函式開啟記事本程式

>>> os.system('notepad')

0 # 關閉記事本後的返回值

# 向記事本傳遞引數,開啟python.txt檔案

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

2 使用shellexecute函式執行其他程式

除了使用os模組中的os.system()函式以外,還可以使用win32api模組中的shellexecute()函式。其函式如下所示。

shellexecute(hwnd, op , file , params , dir , bshow )

其引數含義如下所示。

hwnd:父視窗的控制代碼,如果沒有父視窗,則為0。

op:要進行的操作,為「open」、「print」或者為空。

file:要執行的程式,或者開啟的指令碼。

params:要向程式傳遞的引數,如果開啟的為檔案,則為空。

dir:程式初始化的目錄。

bshow:是否顯示視窗。

以下例項使用shellexecute函式執行其他程式。

>>> import win32api

# 開啟記事本程式,在後台執行,即顯示記事本程式的視窗

>>> win32api.shellexecute(0, 'open', 'notepad.exe', '','',0)

# 開啟記事本程式,在前台執行

>>> win32api.shellexecute(0, 'open', 'notepad.exe', '','',1)

# 向記事本傳遞引數,開啟python.txt

>>> win32api.shellexecute(0, 'open', 'notepad.exe', 'python.txt','',1)

# 在預設瀏覽器中開啟**

>>> win32api.shellexecute(0, 'open', '', '','',1)

>>> win32api.shellexecute(0, 'open', 'e:\\song.wma', '','',1)

# 執行位於e:\book\code目錄中的messagebox.py指令碼

>>> win32api.shellexecute(0, 'open', 'e:\\book\\code\\messagebox.py', '','',1)

可以看出,使用shellexecute函式,就相當於在資源管理器中雙擊檔案圖示一樣,系統會開啟相應的應用程式執行操作。

3 使用createprocess函式執行其他程式

為了便於控制通過指令碼執行的程式,可以使用win32process模組中的createprocess()函式。其函式原型如下所示。

threadattributes , binherithandles ,

dwcreationflags , newenvironment , currentdirectory , startupinfo )

其引數含義如下。

commandline:命令列引數。

processattributes:程序安全屬性,如果為none,則為預設的安全屬性。

threadattributes:執行緒安全屬性,如果為none,則為預設的安全屬性。

binherithandles:繼承標誌。

dwcreationflags:建立標誌。

newenvironment:建立程序的環境變數。

currentdirectory:程序的當前目錄。

startupinfo :建立程序的屬性。

以下例項使用win32process.createprocess函式執行記事本程式。

>>> import win32process

>>> win32process.createprocess('c:\\windows\\notepad.exe', '', 

none , none , 0 ,win32process. create_no_window , none , none ,

win32process.startupinfo())

(<?xml:namespace prefix = pyhandle />, , 280, 3076) 

# 函式返回程序控制代碼、執行緒控制代碼、程序id,以及執行緒id

有了已建立程序的控制代碼就可以使用win32process.terminateprocess函式結束程序,或者使用win32event.waitforsingleobject等待建立的執行緒結束。其函式原型分別如下。

terminateprocess(handle, exitcode)

waitforsingleobject(handle, milliseconds )

對於terminateprocess引數含義分別如下。

handle:要操作的程序控制代碼。

exitcode:程序退出**。

對於waitforsingleobject引數含義分別如下。

handle:要操作的程序控制代碼。

milliseconds:等待的時間,如果為1,則一直等待。

以下例項實現建立程序後並對其進行操作。

>>> import win32process

# 開啟記事本程式,獲得其控制代碼

>>> handle = win32process.createprocess('c:\\windows\\notepad.exe',

'', none , none , 0 ,win32process. create_no_window , none , none ,

win32process.startupinfo())

# 使用terminateprocess函式終止記事本程式

>>> win32process.terminateprocess(handle[0],0)

# 匯入win32event模組

>>> import win32event

# 建立程序獲得控制代碼

>>> handle = win32process.createprocess('c:\\windows\\notepad.exe',

'', none , none , 0 ,win32process. create_no_window , none , none ,

win32process.startupinfo())

# 等待程序結束

>>> win32event.waitforsingleobject(handle[0], -1)

0 # 程序結束的返回值

4 使用ctypes呼叫kernel32.dll中的函式

1.ctypes簡介

ctypes為python提供了呼叫動態鏈結庫中函式的功能。使用ctypes可以方便地呼叫由c語言編寫的動態鏈結庫,並向其傳遞引數。ctypes定義了c語言中的基本資料型別,並且可以實現c語言中的結構體和聯合體。ctypes可以工作在windows、windows ce、mac os x、linux、solaris、freebsd、openbsd等平台上,基本上實現了跨平台。

以下的例項使用ctypes實現了在windows下直接呼叫user32.dll中的messageboxa函式。執行後如圖10-6所示。

>>> from ctypes import *

>>> user32 = windll.loadlibrary('user32.dll') # 載入動態鏈結庫

>>> user32.messageboxa(0, 'ctypes is cool!', 'ctypes', 0)

# 呼叫messageboxa函式

**:

Python呼叫(執行)外部程式

1 使用os.system函式執行其他程式 2 使用shellexecute函式執行其他程式 3 使用createprocess函式執行其他程式 4 使用ctypes呼叫kernel32.dll中的函式 1 使用os.system函式執行其他程式 os模組中的system 函式可以方便地執行其他程式...

Python呼叫(執行)外部程式

在python中可以方便地使用os模組執行其他的指令碼或者程式,這樣就可以在指令碼中直接使用其他指令碼,或者程式提供的功能,而不必再次編寫實現該功能的 為了更好地控制執行的程序,可以使用win32process模組中的函式。如果想進一步控制程序,則可以使用ctype模組,直接呼叫kernel32.d...

Python呼叫(執行)外部程式

python呼叫 執行 外部程式 在python中可以方便地使用os模組執行其他的指令碼或者程式,這樣就可以在指令碼中直接使用其他指令碼,或者程式提供的功能,而不必再次編寫實現該功能的 為了更好地控制執行的程序,可以使用win32process模組中的函式。如果想進一步控制程序,則可以使用ctype...