【问题标题】:Obtaining All Properties of a File in Python在 Python 中获取文件的所有属性
【发布时间】:2020-06-30 20:24:25
【问题描述】:

我正在尝试从 python 中的文件中获取文件属性(如“名称、修改日期”、“类型”、“大小”等...)。该属性称为“SW Last saved with”(单击示例)。此属性告诉您保存模型的 Solidworks 版本。

经过一些研究,这个“SW last saved with”属性似乎是通过注册一个 .DLL 文件 (sldwinshellextu.dll) 添加到 Windows 资源管理器中的。

有没有办法使用一些 python 函数来获取这个特定的文件属性,例如 (file.getProperty("SW last saved with"))?

【问题讨论】:

    标签: python file dll properties solidworks


    【解决方案1】:

    要从 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 以及如何在这种语言中使用它一无所知。但也许我可以为您指明一些方向。

    【讨论】:

      【解决方案2】:

      所以我在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 命令获取所有文件属性。我们在 powershell 中使用 subprocess.Popen() 编写了一些快速命令,然后从 STDOUT 进行 PIPE。

      【讨论】:

      • 您应该知道,只有在运行 powershell 进程的计算机上安装了 Solidworks 时,此方法才有效。
      • 是的,这是我在找到此修复程序时认识到的。无论如何,对于任何试图获取其他文件属性的人来说,它仍然是一些有用的信息。
      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 2022-01-13
      • 2019-06-13
      • 2021-04-29
      • 2019-01-12
      • 2020-05-12
      • 1970-01-01
      相关资源
      最近更新 更多