Reading the target of a .lnk file in Python?

前端 未结 7 2113
悲&欢浪女
悲&欢浪女 2020-11-28 04:12

I\'m trying to read the target file/directory of a shortcut (.lnk) file from Python. Is there a headache-free way to do it? The .lnk spec [PDF] is way over my h

7条回答
  •  无人及你
    2020-11-28 05:03

    I didn't really like any of the answers available because I didn't want to keep importing more and more libraries and the 'shell' option was spotty on my test machines. I opted for reading the ".lnk" in and then using a regular expression to read out the path. For my purposes, I am looking for pdf files that were recently opened and then reading the content of those files:

    # Example file path to the shortcut
    shortcut = "shortcutFileName.lnk"
    
    # Open the lnk file using the ISO-8859-1 encoder to avoid errors for special characters
    lnkFile = open(shortcut, 'r', encoding = "ISO-8859-1")
    
    # Use a regular expression to parse out the pdf file on C:\
    filePath = re.findall("C:.*?pdf", pdfFile.read(), flags=re.DOTALL)
    
    # Close File
    lnkFile.close()
    
    # Read the pdf at the lnk Target
    pdfFile = open(tmpFilePath[0], 'rb')
    

提交回复
热议问题