如何在Python中使用Win32 API?

如何在Python中使用Win32 API?,第1张

如何在Python中使用Win32 API?

PyWin32是必经之路-但是如何使用它呢?一种方法是从遇到的具体问题开始并尝试解决它。PyWin32提供了许多Win32
API函数的绑定,您确实必须首先选择一个特定的目标。

在我的Python 2.5安装中(在Windows上为ActiveState),win32软件包具有一个Demos文件夹,其中包含库各个部分的示例代码。

例如,这是CopyFileEx.py:

import win32file, win32apiimport osdef 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_CONTINUEtemp_dir=win32api.GetTempPath()fsrc=win32api.GetTempFileName(temp_dir,'cfe')[0]fdst=win32api.GetTempFileName(temp_dir,'cfe')[0]print fsrc, fdstf=open(fsrc,'w')f.write('xxxxxxxxxxxxxxxxn'*32768)f.close()## add a couple of extra data streamsf=open(fsrc+':stream_y','w')f.write('yyyyyyyyyyyyyyyyn'*32768)f.close()f=open(fsrc+':stream_z','w')f.write('zzzzzzzzzzzzzzzzn'*32768)f.close()operation_desc='Copying '+fsrc+' to '+fdstwin32file.CopyFileEx(fsrc, fdst, ProgressRoutine, operation_desc, False,   win32file.COPY_FILE_RESTARTABLE)

它显示了如何将CopyFileEx函数与其他几个函数一起使用(例如GetTempPath和GetTempFileName)。从这个例子中,您可以对如何使用该库有一个“一般的感觉”。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5631714.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存