Modify Windows shortcuts using Python

前端 未结 5 699
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 21:46

How do you change a Windows shortcut using Python?

e.g. from:

H:\\My Music\\some_file.mp3

to:

D:\\Users\\Myself\\My         


        
5条回答
  •  时光说笑
    2020-12-09 22:07

    Here's another, more appropriate way to do this in Python with Winshell library: Using Python to create Windows shortcuts. In your case the code will look like:

    import os, winshell
    from win32com.client import Dispatch
    
    desktop = winshell.desktop()
    path = os.path.join(desktop, "some_file.mp3.lnk")
    target = r"D:\Users\Myself\My Music\some_file.mp3"
    wDir = r"D:\Users\Myself\My Music"
    icon = r"D:\Users\Myself\My Music\some_file.mp3"
    
    shell = Dispatch('WScript.Shell')
    shortcut = shell.CreateShortCut(path)
    shortcut.Targetpath = target
    shortcut.WorkingDirectory = wDir
    shortcut.IconLocation = icon
    shortcut.save()
    

    Existing shortcut should be deleted or rewritten. If you need it for batch processing of shortcut files then I think there's some way to read paths from existing shortcuts, but didn't managed to find it.

提交回复
热议问题