8
voussoirkit/winwhich.py at master · voussoir/voussoirkit · GitHub
source link: https://github.com/voussoir/voussoirkit/blob/master/voussoirkit/winwhich.py
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up29 lines (24 sloc) 974 Bytes
''' | |
Instead of adding every program's directory to my system PATH, I prefer having | |
a single directory on my PATH which contains Windows shortcuts (lnk files) | |
pointing to each program. | |
Windows lnks are different from softlinks because they maintain the program's | |
real directory necessary for loading nearby dlls etc. | |
However, this breaks `shutil.which` --> `subprocess.run` because subprocess.run | |
with shell=False does not interpret lnks, and needs a direct path to the exe. | |
So, this module provides a function `which` that if it finds an lnk file, will | |
return the exe path, otherwise behaves the same as normal shutil which. | |
''' | |
import os | |
import shutil | |
try: | |
import winshell | |
except ImportError: | |
winshell = None | |
def which(cmd, *args, **kwargs): | |
path = shutil.which(cmd, *args, **kwargs) | |
if path is None: | |
return None | |
if os.name == 'nt' and winshell and path.lower().endswith('.lnk'): | |
path = winshell.Shortcut(path).path | |
return path |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK