3

I looked over the inetrnet but found nothing about this so I ask here - Is it possible to create a shortcut of a file and put it in a specific directory of my choose with python? For example, I have a folder named "EXAMPLE" in ' C: ' . I want to create automatically a shortcut of Google Chrome and put it in this folder. Is it possible to do so using python(and not just dragging it over by myself)? Thanks

ErezProductions
  • 41
  • 3
  • 11

1 Answers1

6

I assume because of you mention C: that you're using windows. So you can use winshell

import os, winshell
from win32com.client import Dispatch

desktop = winshell.desktop()
path = os.path.join(desktop, "Media Player Classic.lnk")
target = r"P:\Media\Media Player Classic\mplayerc.exe"
wDir = r"P:\Media\Media Player Classic"
icon = r"P:\Media\Media Player Classic\mplayerc.exe"

shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()

For more info look at here

If you're on a unix-based system you can use the symbolic link command.

zom-pro
  • 1,571
  • 2
  • 16
  • 32
  • Thanks! In addition, is it possible to check if the shortcut is already exsits before creating a new one? So that there will be only 1 shortcut and not more than 1 for the same file..? – ErezProductions Aug 22 '15 at 10:10
  • It's just a file so you can do it like this: http://stackoverflow.com/questions/82831/check-whether-a-file-exists-using-python – zom-pro Aug 22 '15 at 10:11
  • Thank you very much! I had an issue where I was doing var = r"link\\to\\path" where I was adding two \\ instead of 1. So if you want the folder shortcut to work, make sure your path is set to path = r"path\to\folder" where you only have 1 \ , not \\. Hope it helps anyone who gets stuck in this like I did. – kanjas Jun 14 '22 at 06:46