Reputation: 145
I have a python script that helps me to manage shortcuts to my various projects by creating directories and shortcut files. I have been using pywin32 and python 10 for the past few months and it has worked fine, but recently I've come across an error that doesn't seem to apply to my situation and wasn't there before. When I noticed this, I had just switched editors from VSCode to PyCharm and made some formatting changes, though neither of those actions seems related to this problem, especially since I'm running the file through command prompt rather than either IDE (though the error also occurs when I run through the IDE).
My program will generate a file path that looks like this:
C:\\Users\<User>\Desktop\Local Projects\Shortcuts\Folder\test.lnk
and I will get an error with this message every time:
File "C:\Users\<User>\OneDrive - <Organization>\Desktop\Local Projects\create new.py", line 594, in <module>
shortcut = shell.CreateShortCut(shortcut_path)
File "<COMObject WScript.Shell>", line 2, in CreateShortCut
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'WshShell.CreateShortcut', 'The
shortcut pathname must end with .lnk or .url.', None, 0, -2147352567), None)
The error message seems to indicate that the pathname does not end in ".lnk" or ".url", but I have confirmed that the variable does end in ".lnk" when this error occurs. I've attempted to reinstall the package multiple times, to no avail.
Something to note is that my desktop folder ("C:\Users\\Desktop") is actually a junction that I configured to redirect to the OneDrive path. This has not been a problem with the script before.
Upvotes: 1
Views: 640
Reputation: 4860
As the error message says, the shortcut pathname must end with .lnk or .url. You have code where it ends in either a space or a double quote.
I've edited your code to fix these issues, and made your filepath strings raw strings so you don't need to escape the backslashes.
if makeShortcut:
shortcut_path = ""
if isAssignment:
shortcut_path = fr"C:\Users\User\Desktop\{shortcuts_folder}\{modified_class_name}\Assignments\{shortcut_rel_path}{project_name}.lnk"
else:
shortcut_path = fr"C:\Users\User\Desktop\{shortcuts_folder}\{modified_class_name}\{shortcut_rel_path}{project_name}.lnk"
os.makedirs(os.path.dirname(shortcut_path), exist_ok=True)
if os.path.exists(shortcut_path):
os.remove(shortcut_path)
print(shortcut_path)
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(shortcut_path)
if editor.lower() == "vscode":
shortcut.TargetPath = fr"C:\Users\User\AppData\Local\Programs\Microsoft VS Code\Code.exe"
elif editor.lower() == "intellij":
shortcut.TargetPath = f"idea"
# set the icon to the intellij icon
shortcut.IconLocation = fr"C:\Taskbar\Icons\idea.ico"
elif editor.lower() == "pycharm":
shortcut.TargetPath = f"pycharm"
# set the icon to the pycharm icon
shortcut.IconLocation = fr"C:\Taskbar\Icons\pycharm.ico"
shortcut.Arguments = f"{folder_path}"
shortcut.save()
if isGithub:
git_shortcut_path = fr"C:\Users\User\Desktop\Local Projects\Shortcuts\{class_name}\Github.lnk"
if os.path.exists(git_shortcut_path):
os.remove(git_shortcut_path)
git_shortcut = shell.CreateShortCut(git_shortcut_path)
git_shortcut.TargetPath = fr"C:\Users\User\Desktop\{shortcuts_folder}\{modified_class_name}"
git_shortcut.save()
Upvotes: 1