stouputils.io.redirect module#
- is_junction(path: str) bool[source]#
Check if a path is a junction point (Windows) or a symlink (any OS).
- Parameters:
path (str) – The path to check
- Returns:
True if the path is a junction or symlink
- Return type:
bool
- create_junction(source: str, target: str) None[source]#
Create a directory junction on Windows pointing source -> target. Uses ‘mklink /J’ command.
- Parameters:
source (str) – The junction path to create (with forward slashes, will be converted)
target (str) – The target directory the junction points to
- create_bind_mount(source: str, target: str) None[source]#
Create a bind mount on Linux pointing source -> target. Uses
mount --bindcommand (requires root/sudo).Note
Bind mounts do not persist across reboots unless an entry is added to
/etc/fstab. To make it persistent, add this line to/etc/fstab:/absolute/path/to/target /absolute/path/to/source none bind 0 0
- Parameters:
source (str) – The mount point path to create (must exist as an empty directory)
target (str) – The target directory to bind
- Raises:
OSError – If the bind mount command fails
- copytree_with_progress(
- source: str,
- destination: str,
- desc: str = 'Copying',
Copy a directory tree from source to destination with a colored progress bar.
Uses
colored_for_loop()to display progress while copying each file. Directory structure is created automatically. Existing files at the destination are overwritten.- Parameters:
source (str) – Path to the source directory to copy
destination (str) – Path to the destination directory
desc (str) – Description for the progress bar (default:
"Copying")
- Returns:
The destination path
- Return type:
str
- Raises:
NotADirectoryError – If source is not a directory
Examples
> copytree_with_progress("C:/Games/MyGame", "D:/Backup/MyGame") # Copying: 100%|██████████████████| 150/150 [00:05<00:00, 30.00it/s] 'D:/Backup/MyGame'
- redirect_folder(
- source: str,
- destination: str,
- link_type: str | None = None,
Move a folder from source to destination and create a link at the original source location.
If the source is already a symlink or junction, the operation is skipped. If the destination path ends with
/, the source folder’s basename is appended automatically.- Link types:
"junction"(or"hardlink"): Uses an NTFS junction on Windows,or a bind mount (
mount --bind, requires sudo) on Linux. Falls back to symlink if junction/bind mount creation fails.
"symlink": Uses a symbolic link (may require elevated privileges on Windows).None: Prompts the user interactively.
- Parameters:
source (str) – Path to the existing folder to redirect
destination (str) – Path where the folder contents will be moved to
link_type (str | None) –
"hardlink"/"junction","symlink", or None to ask
- Returns:
The final destination path
- Return type:
str
Examples
> redirect_folder("C:/Games/MyGame", "D:/Games/") # Moves C:/Games/MyGame -> D:/Games/MyGame and creates a link at C:/Games/MyGame > redirect_folder("C:/Games/MyGame", "D:/Storage/MyGame") # Moves C:/Games/MyGame -> D:/Storage/MyGame and creates a link at C:/Games/MyGame