获取 Python 中文件的所有属性

Obtaining All Properties of a File in Python

我正在尝试从 [=25] 中的文件中获取文件 属性(如 "Name, Date modified"、"Type"、"Size" 等...) =]. 属性 称为“SW Last saved with”(例如单击)。 属性 告诉您保存模型时使用的 Solidworks 版本。

经过一番研究,似乎这个 "SW last saved with" 属性 是通过注册 .DLL 文件 (sldwinshellextu.dll) 添加到 Windows Explorer 的。

有没有办法使用像 (file.getProperty("SW last saved with")) 这样的 python 函数来抓取这个特定文件 属性?

要从 SOLIDWORKS 文件访问文件属性或一些其他信息而不直接在 SW 中打开文件,您可以使用 "SwDocumentManager.dll"。使用此 API,您可以使用 "GetDocument()" 访问特定文件并获取 ISwDMDocument 对象。采用 阅读 属性 "LastSavedDate" 以获取您的信息。

ISwDMDocument 对象的对象信息: https://help.solidworks.com/2020/English/api/swdocmgrapi/SolidWorks.Interop.swdocumentmgr~SolidWorks.Interop.swdocumentmgr.ISwDMDocument_members.html

关于如何使用 SwDocumentManager 的一般信息API: https://help.solidworks.com/2020/English/api/swdocmgrapi/HelpViewerDS.aspx?version=2020&prod=api&lang=English&path=swdocmgrapi%2fGettingStarted-swdocmgrapi.html&id=74236490007f4b5eb5ba233479f1e707

但我对 python 以及如何在这种语言中使用它一无所知。但也许我可以为您指明方向。

所以我在 another post I found:

的帮助下想出了一个方法来做到这一点
import subprocess
newCOMObjectTxt = ("$path = 'PATH_TO_SLDPRT_FILE';"
         "$shell = New-Object -COMObject Shell.Application;"
         "$folder = Split-Path $path;"
         "$file = Split-Path $path -Leaf;"
         "$shellfolder= $shell.Namespace($folder);"
         "$shellfile = $shellfolder.ParseName($file);")
 swLastSavedWithIdx = None
 swFindLastSavedWithProp = subprocess.Popen (["powershell.exe", newCOMObjectTxt + \
        "0..500 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_)}"],
        stdout = subprocess.PIPE)
  while True:
     line = swFindLastSavedWithProp.stdout.readline()
     if b"SW Last saved with" in line:
        swLastSavedWithIdx = int(line.split()[0])
        break
     if not line:
        break
  swLastSaveWithVersion = subprocess.Popen (["powershell.exe", newCOMObjectTxt + \
        "$shellfolder.GetDetailsOf($shellfile, %i)" %swLastSavedWithIdx], stdout = subprocess.PIPE)
  ver = str(swLastSaveWithVersion.stdout.readline(),'utf-8').strip()

基本上我发现您可以通过一些 Windows Powershell 命令获取所有文件属性。我们使用 subprocess.Popen() 在 powershell 中编写一些快速命令,然后使用 STDOUT 中的 PIPE。