python打开快捷方式_python怎么通过快捷方式访问到实际的target路径?

归根结底,快捷方式只是一个二进制文件,你直接用打开文件的方式,读取你需要的信息即可,请看这篇文章:Windows快捷方式文件格式详解(附代码) - 淡泊明志,宁静致远 - ITeye博客

用二进制方式获取:

import struct

def get_lnk_file(path):

target = ''

with open(path, 'rb') as stream:

content = stream.read()

# skip first 20 bytes (HeaderSize and LinkCLSID)

# read the LinkFlags structure (4 bytes)

lflags = struct.unpack('I', content[0x14:0x18])[0]

position = 0x18

# if the HasLinkTargetIDList bit is set then skip the stored IDList

# structure and header

if (lflags & 0x01) == 1:

position = struct.unpack('H', content[0x4C:0x4E])[0] + 0x4E

last_pos = position

position += 0x04

# get how long the file information is (LinkInfoSize)

length = struct.unpack('I', content[last_pos:position])[0]

# skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlags, and VolumeIDOffset)

position += 0x0C

# go to the LocalBasePath position

lbpos = struct.unpack('I', content[position:position+0x04])[0]

position = last_pos + lbpos

# read the string at the given position of the determined length

size= (length + last_pos) - position - 0x02

temp = struct.unpack('c' * size, content[position:position+size])

target = ''.join([chr(ord(a)) for a in temp])

return target

print(get_lnk_file("d:/demo.lnk"))

当然,也可以通过调windows的api获取文件属性信息。

import sys

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")

shortcut = shell.CreateShortCut("d:\\demo.lnk")

print(shortcut.Targetpath)

以上代码在python3.6下调试通过

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值