-3
The code below should run with administrative privileges.
If the question is how to get a python script to create a link from you and send it into the global startup folder on windows OS:
- first your script will have to discover the path to OS startup folder.
- then you have to create a link for yourself and put it in the startup folder.
To find the path to the startup folder it is possible to do a search on the record of the machine on which the script is running and extract the input. The key to the registry "HKEY_LOCAL_MACHINE Software Microsoft Windows Currentversion Explorer Shell Folders" contains the paths to shell:folders which are special paths in the windows environment. Among these paths is the path to shell:Common Startup that the boot folder of for all Operating System users.
Aware of this the program should then connect to the registry via the function winreg.ConnectRegistry()
in the key winreg.HKEY_LOCAL_MACHINE
and with the function winreg.OpenKey()
open the key "Microsoft Windows Currentversion Explorer Shell Folders Software" and then count the number of values of that key with the function winreg.QueryInfoKey()
and then enumerate them with winreg.EnumValue()
and salvalos in a dictionary shell
.
To create the link do with the function os.symlink()
and pass as parameters the source the absolute path to the global __file__
and as fate shell["Common Startup"]
which is value containing the path to the operating system startup folder.
import os
import winreg
shell ={}
with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as reg:
with winreg.OpenKey(reg, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") as key:
for i in range(winreg.QueryInfoKey(key)[1]):
nome, val, _ = winreg.EnumValue(key, i)
shell[nome] = val
os.symlink(os.path.abspath(__file__), f"{shell['Common Startup']}\\teste.py")
If the question is how to make a python script create a link from you and send it inside the particular startup folder of the user that runs the script on the windows operating system, the process is even but changes some details changes the registration key to be searched and value name becomes only Startup
.
import os
import winreg
shell ={}
with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as reg:
with winreg.OpenKey(reg, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") as key:
for i in range(winreg.QueryInfoKey(key)[1]):
nome, val, _ = winreg.EnumValue(key, i)
shell[nome] = val
os.symlink(os.path.abspath(__file__), f"{shell['Startup']}\\teste.py")
If the question is not about self-copying when creating the link just change the source cumin by the file path you want to create the link.
For the sake of argument: to) you want to create the shortcut of a file USING the Python language or b) you want to create the SHORTCUT OF YOUR Python script to run it while starting Windows? The question "create shortcut with Python" suggests that you want option A, but in the text you put "create a shortcut for my . py", which suggests option B.
– Woss
See if that helping
– Paulo Marques
Your question is a little dubious. In the first part it refers to a SHORTCUT. In the second part it gives us the impression that you want to insert a script
.py
in the Windows startup folder - perhaps - wanting the said script to boot every time the system boots. I suggest you edit your question making your intention clearer.– Solkarped