path Module (stx.path)
scitex-path: Scientific project path utilities (find, split, symlink, versioning).
- scitex.path.clean(path_string)[source]
Cleans and normalizes a file system path string.
Example
>>> clean('/home/user/./folder/../file.txt') '/home/user/file.txt' >>> clean('path/./to//file.txt') 'path/to/file.txt' >>> clean('path with spaces') 'path_with_spaces'
- scitex.path.create_relative_symlink(src, dst, overwrite=False)[source]
Create a relative symbolic link.
This is a convenience wrapper around symlink() with relative=True.
- scitex.path.find_git_root()[source]
Find the root directory of the current git repository.
- Returns:
Path to the git repository root.
- Return type:
- scitex.path.find_latest(dirname, fname, ext, version_prefix='_v')[source]
Find the latest versioned file in a directory.
- Parameters:
- Returns:
Path to the latest versioned file, or None if not found.
- Return type:
str or None
- scitex.path.fix_broken_symlinks(directory, recursive=False, remove=False, new_target=None)[source]
Find and optionally fix broken symbolic links.
- Parameters:
- Return type:
- Returns:
Dictionary with ‘found’, ‘fixed’, and ‘removed’ lists of paths
- scitex.path.get_data_path_from_a_package(package_str, resource)[source]
Get the path to a data file within a package.
- Parameters:
- Returns:
The full path to the resource file.
- Return type:
Path
- Raises:
ImportError – If the specified package cannot be found.
FileNotFoundError – If the resource file does not exist in the package’s data directory.
- scitex.path.get_spath(sfname, makedirs=False)
Create a save path based on the calling script’s location.
- Parameters:
- Returns:
The full save path for the file.
- Return type:
Example
>>> spath = mk_spath('output.txt', makedirs=True)
- scitex.path.get_this_path(ipython_fake_path='/tmp/fake.py')
Get the path of the calling script.
Note
This function historically captures the caller’s filename via
inspect.stack()[1]but then returns this module’s__file__. The tests codify that legacy behavior; do not change without updating callers.- Return type:
- scitex.path.getsize(path)[source]
Get file size in bytes.
- Parameters:
path (str or Path) – Path to file.
- Returns:
File size in bytes, or math.nan if file doesn’t exist.
- Return type:
- Raises:
PermissionError – If the file cannot be accessed due to permissions.
- scitex.path.increment_version(dirname, fname, ext, version_prefix='_v')[source]
Generate the next version of a filename based on existing versioned files.
- Parameters:
- Returns:
Full path for the next version of the file.
- Return type:
Example
>>> increment_version('/path/to/dir', 'myfile', '.txt') '/path/to/dir/myfile_v001.txt'
- scitex.path.list_symlinks(directory, recursive=False)[source]
List all symbolic links in a directory.
- scitex.path.mk_spath(sfname, makedirs=False)[source]
Create a save path based on the calling script’s location.
- Parameters:
- Returns:
The full save path for the file.
- Return type:
Example
>>> spath = mk_spath('output.txt', makedirs=True)
- scitex.path.split(fpath)[source]
Split a file path into directory, filename, and extension.
- Parameters:
fpath (str or Path) – File path to split.
- Returns:
(directory with trailing slash, filename without extension, extension)
- Return type:
Example
>>> dirname, fname, ext = split('/path/to/file.txt') >>> dirname '/path/to/' >>> fname 'file' >>> ext '.txt'
- scitex.path.symlink(src, dst, overwrite=False, target_is_directory=None, relative=False)[source]
Create a symbolic link pointing to src named dst.
- Parameters:
src (
Union[str,Path]) – Source path (target of the symlink)dst (
Union[str,Path]) – Destination path (the symlink to create)overwrite (
bool) – If True, remove existing dst before creating symlinktarget_is_directory (
Optional[bool]) – On Windows, specify if target is directory (auto-detected if None)relative (
bool) – If True, create relative symlink instead of absolute
- Return type:
- Returns:
Path object of the created symlink
- Raises:
FileExistsError – If dst exists and overwrite=False
FileNotFoundError – If src doesn’t exist
OSError – If symlink creation fails
Examples
>>> from scitex_path import symlink >>> # Create absolute symlink >>> symlink("/path/to/source", "/path/to/link")
>>> # Create relative symlink >>> symlink("../source", "link", relative=True)
>>> # Overwrite existing symlink >>> symlink("/path/to/new_source", "/path/to/link", overwrite=True)
- scitex.path.this_path(ipython_fake_path='/tmp/fake.py')[source]
Get the path of the calling script.
Note
This function historically captures the caller’s filename via
inspect.stack()[1]but then returns this module’s__file__. The tests codify that legacy behavior; do not change without updating callers.- Return type: