0

I have variables declared for a path to place a shortcut to the public desktop (code for that below)

SOURCE_PATH = r"\\ServerAddress\Installs\Software Package"
DEST_PATH = r"C:\Users\Public\Desktop"
FILE_NAME = "\\ProgramShortcut.lnk"
def place_shortcut():
 print("Placing Shortcut on Desktop..")
 shutil.copyfile(SOURCE_PATH + FILE_NAME, DEST_PATH + FILE_NAME) # 

I am looking to use those same variable(DEST_PATH and File_Name) to remove that same shortcut at the same spot - just to give you a idea what I am trying to do is basically the program will remove the icon/remove the program/reinstall program and then place the shortcut back using same variables. When I use the following code below it does not seem to do anything.

def remove_shortcut():
 if os.path.isfile(os.path.join(DEST_PATH, FILE_NAME)):
 os.remove(os.path.join(DEST_PATH, FILE_NAME))
 print("Removing existing shortcuts")
asked May 3, 2019 at 16:39
1
  • See this lovely debug blog for help. Commented May 3, 2019 at 16:58

1 Answer 1

4

From the docs about os.path.join:

If a component [that is, the second argument of the function] is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

(Emphasis mine)

In your case, the call will be something like:

os.path.join(r"C:\Users\Public\Desktop", "\\ProgramShortcut.lnk")

But "\\ProgramShortcut.lnk" is an absolute path, so you'll end up checking the file C:\ProgramShortcut.lnk.

answered May 3, 2019 at 16:47
Sign up to request clarification or add additional context in comments.

2 Comments

I see what you're saying here but its a complete separate function just wanting to make use of the variables again
@JasonLucas, this is impossible with the current values of these variables. A quick fix would be to do FILE_NAME = "ProgramShortcut.lnk". os.path.join will put the necessary separators automatically.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.