My problem is that i can't find a solution to create a shortcut to an folder with python. Only to a file, code example:
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.save()
But I need a shortcut to a whole folder.
My target example is:
C:/Usersand C:/Users/user/Downloads
You can use the below code to create shortcut to a directory
from win32com.client import Dispatch
path = r"C:\Users\user\Downloads\shortcut.lnk" #This is where the shortcut will be created
target = r"C:\Users\user\Downloads" # directory to which the shortcut is created
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.save()
Backslashes (\) must be used in shortcut.Targetpath for shortcuts to work correctly for folders.
Related
I'm writing an installer for a Python app I'm making, and needed to have paths for different file locations. These are what I have so far, and so wondered if these were correct of if I should change any of them (particularly the Linux/macOS ones)
I have a path for:
the actual binary
for the config file
the README
the start menu, should the user want to add a shortcut (this will create the .desktop file on Linux/macOS, a shortcut on Windows)
the desktop, should the user want to add a shortcut
Can anyone let me know if there should be changes to these to make them work on all devices etc
Windows
binary = '%ProgramFiles%'
configs = '%AppData%'
README = '%ProgramFiles%'
start menu = '%AppData%\Microsoft\Windows\Start Menu'
desktop = os.path.expanduser('~\Desktop')
Linux
binary = '/usr/local/bin'
config = os.path.expanduser('~/.config')
README = '/usr/local/share/doc/packages'
start menu = os.path.expanduser('~/.local/share/applications')
desktop = os.path.expanduser('~/Desktop')
macOS
binary = '/usr/local/bin'
config = os.path.expanduser('~/.config')
README = '/usr/local/share/doc/packages'
start menu = os.path.expanduser('~/.local/share/applications')
desktop = os.path.expanduser('~/Desktop')
Thanks in advance
EDIT: I have added the option for the user to change the paths anyway so it doesn't have to be perfect but I would like the best 'normal' paths
My recommendation is to use sysconfig as much as possible.
For example on a POSIX system get_paths() returns:
In [5]: sysconfig.get_paths()
Out[5]:
{'stdlib': '/usr/local/lib/python3.9',
'platstdlib': '/usr/local/lib/python3.9',
'purelib': '/usr/local/lib/python3.9/site-packages',
'platlib': '/usr/local/lib/python3.9/site-packages',
'include': '/usr/local/include/python3.9',
'platinclude': '/usr/local/include/python3.9',
'scripts': '/usr/local/bin',
'data': '/usr/local'}
Note that sysconfig supports different schemes:
In [6]: sysconfig.get_scheme_names()
Out[6]:
('nt',
'nt_user',
'osx_framework_user',
'posix_home',
'posix_prefix',
'posix_user')
The home or user schemes are handy when the installer doesn't have root or administrator privileges.
I am looking to permanently modify the path variable inside windows from a python script. The script is a wrapper to help automate the installation of applications and I want to enable the API to add applications to the path.
So for example I want to install a program called micro which has an install path of C:\Users\USERNAME\Path\to\micro and then add that install path to my path variable so that I can just run micro in my terminal.
I've been made aware of 2 possible solutions, which both do not work:
1. Using os.environ
In python the os module let's you read environment variables, but not actually modify them. so for example:
program_path = "C:\\Users\\USERNAME\\Path\\to\\micro"
new_path = f"{os.environ['PATH']};{program_path}"
os.environ["PATH"] = new_path
This would update the path variable in the python script, but it does not actually modify it on the system which is what I want.
2. setx
I was made aware that it is possible to update your path using the setx command in windows, but for some reason on windows 10 this destroys your path variable.
The idea is that you can call the setx command from python and use it to update the path variable. You should be able to type setx path "%path%;C:\Users\USERNAME\Path\to\micro" and have it update correctly.
So for example, in python code that would be:
program_path = "C:\\Users\\USERNAME\\Path\\to\\micro"
subprocess.Popen(f'setx path "%path%;{program_path}"')
This should take the current path variable and append the program path to it, but instead it just wipes your entire path and replaces it with a literal %path% and then the program path.
So now my path looks like this:
%path%
C:\Users\USERNAME\Path\to\micro
Any ideas on how to get this to work would be appreciated.
Okay, so after a long (and disgusting) amount of research I found a solution. Here is the method I came up with for a cross-platform system of adding to PATH variable:
def add_to_path(program_path:str):
"""Takes in a path to a program and adds it to the system path"""
if os.name == "nt": # Windows systems
import winreg # Allows access to the windows registry
import ctypes # Allows interface with low-level C API's
with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root: # Get the current user registry
with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key: # Go to the environment key
existing_path_value = winreg.EnumValue(key, 3)[1] # Grab the current path value
new_path_value = existing_path_value + program_path + ";" # Takes the current path value and appends the new program path
winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, new_path_value) # Updated the path with the updated path
# Tell other processes to update their environment
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A
SMTO_ABORTIFHUNG = 0x0002
result = ctypes.c_long()
SendMessageTimeoutW = ctypes.windll.user32.SendMessageTimeoutW
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment", SMTO_ABORTIFHUNG, 5000, ctypes.byref(result),)
else: # If system is *nix
with open(f"{os.getenv('HOME')}/.bashrc", "a") as bash_file: # Open bashrc file
bash_file.write(f'\nexport PATH="{program_path}:$PATH"\n') # Add program path to Path variable
os.system(f". {os.getenv('HOME')}/.bashrc") # Update bash source
print(f"Added {program_path} to path, please restart shell for changes to take effect")
Neither are pretty, but it does actually work. You do need to restart running shells for it to take effect, but other than that it's perfect.
I have created shortcuts for executables and it works, but when I try to create one for a folder it does not work.
It does create a shortcut, it is just not the right 'Target Type'. Please take a look at the image below.
Instead of 'File', the target type should be 'File folder'. The problem is that when I open the shortcut it asks me which program do I want to open the File with and it does not open the folder.
The function I'm using to create the shortcuts is the following
from win32com.client import Dispatch
import winshell
import os
def create_shortcuts(self, tool_name, exe_path, startin, icon_path):
shell = Dispatch('WScript.Shell')
shortcut_file = os.path.join(winshell.desktop(), tool_name + '.lnk')
shortcut = shell.CreateShortCut(shortcut_file)
shortcut.Targetpath = exe_path
shortcut.WorkingDirectory = startin
shortcut.IconLocation = icon_path
shortcut.save()
I don't know if it's possible to set the 'Target Type'. I couldn't find a way to do it, but I do know there must be a way.
If you want to use .Net "clr" (especially if you already require it):
First run this... you will have to ship the output of this command with your application:
"c:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\TlbImp.exe" %SystemRoot%\system32\wshom.ocx /out:Interop.IWshRuntimeLibrary.dll
tlbimp.exe might even be in the path if you installed the Windows SDK in a fairly standard way. But if not, it's OK, you'll just ship the "assembly" (fancy word for interface-providing dll in .Net land) with your application.
Then this code will work in python:
import clr
sys.path.append(DIRECTORY_WHERE_YOU_PUT_THE_DLL)
clr.AddReference('Interop.IWshRuntimeLibrary')
import Interop.IWshRuntimeLibrary
sc = Interop.IWshRuntimeLibrary.WshShell().CreateShortcut("c:\\test\\sc.lnk")
isc = Interop.IWshRuntimeLibrary.IWshShortcut(sc)
isc.set_TargetPath("C:\\")
isc.Save()
.... the above code, with far too much modification and preamble, might even work with Mono.
For future reference: I observed the described behavior in python 3.9.6 when creating a shortcut to a non-existing directory, which was easily fixed by incorporating os.makedirs() into the method.
I've added a method parameter to the version I'm using, so it can handle shortcuts to files and directories:
def create_shortcuts(self, tool_name, exe_path, startin, icon_path, is_directory=False):
if is_directory:
os.makedirs(exe_path, exist_ok=True)
shell = Dispatch('WScript.Shell')
shortcut_file = os.path.join(winshell.desktop(), tool_name + '.lnk')
shortcut = shell.CreateShortCut(shortcut_file)
shortcut.Targetpath = exe_path
shortcut.WorkingDirectory = startin
shortcut.IconLocation = icon_path
shortcut.save()
I am using Python and the win32com.client library to run a VBA macro. The macro requires a directory (which is a substring of the .xlsm path itself). How do I pass this directory/string to the VBA popup?
import os, os.path
import win32com.client
if os.path.exists('C:/test_folder/excel_file.xlsm'):
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open('C:/test_folder/excel_file.xlsm', ReadOnly=1)
xl.Application.Run('excel_file.xlsm!Sheet1.Macro1')
After this code is run, the folder explorer window pops up asking for a directory. How would I pass the directory to Excel?
This line of code will help you save the exact directory you want making it non dependent to the python path, and associates it to a variable, after that you can just pass on that string variable onto your vba popup. You can try making it global or just reusing it on the continuation of the code, after that you can just reference so you can put it in excel. I hope this helps.
Sub Selectingadirectory()
Dim fd As FileDialog
Dim ActionClicked As Boolean
Dim SelectedFolderPath As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.title = "Chose your directory path"
fd.InitialFileName = Environ("UserProfile") & "\Desktop"
fd.AllowMultiSelect = False
ActionClicked = fd.Show
If ActionClicked Then
SelectedFolderPath = fd.SelectedItems(1)
Else
Msgbox "You didnt chose a folder"
Exit Sub
End If
End sub
Actually need to go some path and execute some command and below is the code
code:
import os
present_working_directory = '/home/Desktop/folder'
presently i am in folder
if some_condition == true :
change_path = "nodes/hellofolder"
os.chdir(change_path)
print os.getcwd()
if another_condition == true:
change_another_path = "nodes"
os.chdir(change_another_path)
print os.getcwd()
**Result**:
'/home/Desktop/folder/nodes/hellofolder'
python: [Errno 1] No such file or directory
Actually whats happening here is when i first used os.chdir() the directory has changed to
'/home/Desktop/folder/nodes/hellofolder',
but for the second one i need to run a file by moving to one folder back that is
'/home/Desktop/folder/nodes'
So can anyone let me how to move one folder back in python
Just like you would in the shell.
os.chdir("../nodes")
Here is a very platform independent way to do it.
In [1]: os.getcwd()
Out[1]: '/Users/user/Dropbox/temp'
In [2]: os.path.normpath(os.getcwd() + os.sep + os.pardir)
Out[2]: '/Users/user/Dropbox/'
Then you have the path, and you can chdir or whatever with it.
Just call
os.chdir('..')
the same as in any other language :)
Exact answer for your question is os.chdir('../')
Use case:
Folder1:
sub-folder1:(you want to navigate here)
Folder2:
sub-folde2:(you are here)
To navigate to sub-folder1 from sub-folder2, you need to write like this
"../Folder1/sub-folder1/"
then, put it in os.chdir("../Folder1/sub-folder1/").
think about using absolute paths
import os
pwd = '/home/Desktop/folder'
if some_condition == true :
path = os.path.join(pwd, "nodes/hellofolder")
os.chdir(path)
print os.getcwd()
if another_condition == true:
path = os.path.join(pwd, "nodes")
os.chdir(path)
print os.getcwd()
My problem was fixed with this command
first import os and after add
os.path.normpath(os.path.abspath(__file__) + os.sep + os.pardir)
The answers mentioned above are correct. The following is more a
It usually happens when your Python script is in a nested directory and you want to go one level up from the current working directory to maybe let's say load a file.
The idea is to simply reformat the path string and prefix it with a '../'. So an example would be.
'../current_directory/' + filename
This format is similar to when used in a terminal. Whenever in doubt fire up a terminal and experiment with some commands. The format is reflected in the programming language.
Define this function in your script and call it whenever you want to go back just by one folder:
import os
def dirback():
m = os.getcwd()
n = m.rfind("\\")
d = m[0: n+1]
os.chdir(d)
return None