Jason Lucas
Jason Lucas

Reputation: 113

Passing a variable to os.pathisfile

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")

Upvotes: 0

Views: 54

Answers (1)

ForceBru
ForceBru

Reputation: 44926

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.

Upvotes: 4

Related Questions