How to use IFileOperation from ctypes

前端 未结 2 1285
野的像风
野的像风 2020-12-02 00:43

I want to use IFileOperation to copy files from python code -

  • It\'s fast(er than python)
  • You get a nice dialog
  • Doesn\'t block Python
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 01:31

    Sure, it's located in pythoncom and shell for constants, for example:

    from win32com.shell import shell
    import pythoncom
    
    # create an instance of IFileOperation
    fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation)
    
    # here you can use SetOperationFlags, progress Sinks, etc.
    
    # create an instance of IShellItem for the source item
    item1 = shell.SHCreateItemFromParsingName("c:\\temp\\source.txt", None, shell.IID_IShellItem)
    
    # create an instance of IShellItem for the target folder
    folder = shell.SHCreateItemFromParsingName("c:\\another", None, shell.IID_IShellItem)
    
    # queue the copy operation
    fo.CopyItem(item1, folder, "new name.txt", None)
    
    # commit
    fo.PerformOperations()
    

提交回复
热议问题