在Python中获取文件的所有属性

时间:2020-03-19 16:49:29

标签: python file dll properties solidworks

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

经过一番研究,看来该“ SW最后保存为”属性是通过注册.DLL文件(sldwinshellextu.dll)添加到Windows资源管理器的。

是否可以使用某些Python函数(例如(file.getProperty(“ SW最后保存为”的软件“))来获取此特定文件的属性?

2 个答案:

答案 0 :(得分: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以及如何以这种语言使用它一无所知。但是也许我可以指出您的方向。

答案 1 :(得分:1)

因此我想出了一种借助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编写一些快速命令。

相关问题