Reading and editing Windows PATH.
Project description
🛁 pathtub
Simple python functions for reading and editing Windows PATH variables.
✅ Ensuring that an folder exists in Path.
🧽 Cleaning the PATH (duplicates, removed folders, sorting)
✏️ Adding or removing folders to/from Path (temporary or permanently)
Installing
pip install pathtub
Usage
✅ Ensuring folder is in PATH
What is ensure()?
ensure(folder)
checks if folder
is in Process PATH
- If
folder
is in Process PATH, does nothing - If
folder
is not in Process PATH, adds it to Process PATH - If
folder
is not in Process PATH andpermanent=True
, adds also to the User PATH (permanent) or System PATH (permanent, needs admin rights).
Example code for ensure()
- It is safe to call
ensure()
every time you load your script, for example. It only does something iffolder
is not found in your processPATH
. - The last "trailing" backslash of
folder
(if any) is ignored when comparing to any other folders.
from pathtub import ensure
dll_folder = r'C:\my favourite\dlls'
# 1) Check Process PATH, i.e. os.environ['PATH']
# 2) Add to Process PATH (temporary) if not found
ensure(dll_folder)
Example code for ensure() with permanent addition
- You may also make the addition permanent (& visible to other processes).
- Also this is safe to call every time script is starting.
from pathtub import ensure
dll_folder = r'C:\my favourite\dlls'
# 1) Check Process PATH
# 2) Add to Process PATH if not found
# 3) Add also to User PATH (permanent), if 2) happens
ensure(dll_folder, permanent=True)
- The Process PATH is initiated by copying it from parent process or taking copy of union of the permanent (User/System) PATH when process is started. For more info, see: Windows PATH variables.
- More detailed example: Using
pathtub.ensure
to add DLL to PATH. - Full documentation of
ensure()
is in the source code (pathtools.py).
🧽 Cleaning PATH
Cleaning paths means
- Removing duplicates from the PATH (trailing backslash neglected)
- Removing empty entries from PATH
- Sorting alphabetically (optional, Default: True)
- Removing folders that do not exist (optional, Default: True)
- Removing from "User" list the ones that are in the "System" list (optional, default: True)
Screenshots of User PATH before and after clean:
Code example for clean
from pathtub import clean
clean()
# possible parameters:
# clean(sort=True, remove_non_existent=True, remove_user_duplicates=True)
- For more detailed example, see Full example of pathtub.clean
- Full documentation of
clean()
is in the source code (pathtools.py).
Rest of the docs
Did not find what you were looking for? See the Rest of the docs.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pathtub-1.1.1.tar.gz
(6.5 kB
view hashes)