问题描述
如何在Python中使用win32 API?最好和最简单的方法是什么?
您能提供一些例子吗?
How can I use win32 API in Python?What is the best and easiest way to do it?
Can you please provide some examples?
推荐答案
PyWin32是必经之路-但是如何使用它呢?一种方法是从遇到的具体问题开始并尝试解决它. PyWin32提供了许多Win32 API函数的绑定,您确实必须首先选择一个特定的目标.
PyWin32 is the way to go - but how to use it? One approach is to begin with a concrete problem you're having and attempting to solve it. PyWin32 provides bindings for the Win32 API functions for which there are many, and you really have to pick a specific goal first.
在我的Python 2.5安装中(在Windows上为ActiveState),win32软件包具有一个Demos文件夹,其中包含该库各个部分的示例代码.
In my Python 2.5 installation (ActiveState on Windows) the win32 package has a Demos folder packed with sample code of various parts of the library.
例如,这里是CopyFileEx.py:
For example, here's CopyFileEx.py:
import win32file, win32api
import os
def ProgressRoutine(TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred,
StreamNumber, CallbackReason, SourceFile, DestinationFile, Data):
print Data
print TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred, StreamNumber, CallbackReason, SourceFile, DestinationFile
##if TotalBytesTransferred > 100000:
## return win32file.PROGRESS_STOP
return win32file.PROGRESS_CONTINUE
temp_dir=win32api.GetTempPath()
fsrc=win32api.GetTempFileName(temp_dir,'cfe')[0]
fdst=win32api.GetTempFileName(temp_dir,'cfe')[0]
print fsrc, fdst
f=open(fsrc,'w')
f.write('xxxxxxxxxxxxxxxx\n'*32768)
f.close()
## add a couple of extra data streams
f=open(fsrc+':stream_y','w')
f.write('yyyyyyyyyyyyyyyy\n'*32768)
f.close()
f=open(fsrc+':stream_z','w')
f.write('zzzzzzzzzzzzzzzz\n'*32768)
f.close()
operation_desc='Copying '+fsrc+' to '+fdst
win32file.CopyFileEx(fsrc, fdst, ProgressRoutine, operation_desc, False, win32file.COPY_FILE_RESTARTABLE)
它显示了如何将CopyFileEx函数与其他几个函数一起使用(例如GetTempPath和GetTempFileName).从这个例子中,您可以对如何使用该库有一种一般的感觉".
It shows how to use the CopyFileEx function with a few others (such as GetTempPath and GetTempFileName). From this example you can get a "general feel" of how to work with this library.
这篇关于如何在Python中使用Win32 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!