ubelt.util_path module¶
Functions for working with filesystem paths.
-
ubelt.util_path.
augpath
(path, suffix='', prefix='', ext=None, base=None, multidot=False)[source]¶ Augments a path with a new basename, extension, prefix and/or suffix.
A prefix is inserted before the basename. A suffix is inserted between the basename and the extension. The basename and extension can be replaced with a new one.
Parameters: - path (PathLike) – string representation of a path
- suffix (str) – placed between the basename and extension
- prefix (str) – placed in front of the basename
- ext (str) – if specified, replaces the extension
- base (str) – if specified, replaces the basename (without extension)
- multidot (bool) – if False, everything after the last dot in the basename is the extension. If True, everything after the first dot in the basename is the extension (Defaults to False).
Returns: augmented path
Return type: PathLike
- CommandLine:
- python -m ubelt.util_path augpath
Example
>>> import ubelt as ub >>> path = 'foo.bar' >>> suffix = '_suff' >>> prefix = 'pref_' >>> ext = '.baz' >>> newpath = ub.augpath(path, suffix, prefix, ext=ext, base='bar') >>> print('newpath = %s' % (newpath,)) newpath = pref_bar_suff.baz
Example
>>> augpath('foo.bar') 'foo.bar' >>> augpath('foo.bar', ext='.BAZ') 'foo.BAZ' >>> augpath('foo.bar', suffix='_') 'foo_.bar' >>> augpath('foo.bar', prefix='_') '_foo.bar' >>> augpath('foo.bar', base='baz') 'baz.bar' >>> augpath('foo.tar.gz', ext='.zip', multidot=True) foo.zip >>> augpath('foo.tar.gz', ext='.zip', multidot=False) foo.tar.zip
-
ubelt.util_path.
userhome
(username=None)[source]¶ Returns the user’s home directory. If username is None, this is the directory for the current user.
Parameters: username (str) – name of a user on the system Returns: userhome_dpath: path to the home directory Return type: PathLike Example
>>> import getpass >>> username = getpass.getuser() >>> assert userhome() == expanduser('~') >>> assert userhome(username) == expanduser('~')
-
ubelt.util_path.
compressuser
(path, home='~')[source]¶ Inverse of os.path.expanduser
Parameters: - path (PathLike) – path in system file structure
- home (str) – symbol used to replace the home path. Defaults to ‘~’, but you might want to use ‘$HOME’ or ‘%USERPROFILE%’ instead.
Returns: path: shortened path replacing the home directory with a tilde
Return type: PathLike
- CommandLine:
- xdoctest -m ubelt.util_path compressuser
Example
>>> path = expanduser('~') >>> assert path != '~' >>> assert compressuser(path) == '~' >>> assert compressuser(path + '1') == path + '1' >>> assert compressuser(path + '/1') == join('~', '1') >>> assert compressuser(path + '/1', '$HOME') == join('$HOME', '1')
-
ubelt.util_path.
expandpath
(path)[source]¶ Wrapper around expanduser and expandvars.
Less aggressive than truepath. Only expands environs and tilde. Does not change relative paths to absolute paths.
Parameters: path (PathLike) – string representation of a path Returns: expanded path Return type: PathLike Example
>>> import ubelt as ub >>> assert normpath(ub.expandpath('~/foo')) == join(ub.userhome(), 'foo') >>> assert ub.expandpath('foo') == 'foo'
-
ubelt.util_path.
truepath
(path, real=False)[source]¶ Normalizes a string representation of a path and does shell-like expansion.
Parameters: - path (PathLike) – string representation of a path
- real (bool) – if True, all symbolic links are followed. (default: False)
Returns: normalized path
Return type: PathLike
Note
This function is similar to the composition of expanduser, expandvars, normpath, and (realpath if real else abspath). However, on windows backslashes are then replaced with forward slashes to offer a consistent unix-like experience across platforms.
On windows expanduser will expand environment variables formatted as %name%, whereas on unix, this will not occur.
- CommandLine:
- python -m ubelt.util_path truepath
Example
>>> import ubelt as ub >>> assert ub.truepath('~/foo') == join(ub.userhome(), 'foo') >>> assert ub.truepath('~/foo') == ub.truepath('~/foo/bar/..') >>> assert ub.truepath('~/foo', real=True) == ub.truepath('~/foo')
-
ubelt.util_path.
ensuredir
(dpath, mode=1023, verbose=None)[source]¶ Ensures that directory will exist. Creates new dir with sticky bits by default
Parameters: - dpath (PathLike) – dir to ensure. Can also be a tuple to send to join
- mode (int) – octal mode of directory (default 0o1777)
- verbose (int) – verbosity (default 0)
Returns: path: the ensured directory
Return type: PathLike
Notes
This function is not thread-safe in Python2
Example
>>> from ubelt.util_platform import * # NOQA >>> import ubelt as ub >>> cache_dpath = ub.ensure_app_cache_dir('ubelt') >>> dpath = join(cache_dpath, 'ensuredir') >>> if exists(dpath): ... os.rmdir(dpath) >>> assert not exists(dpath) >>> ub.ensuredir(dpath) >>> assert exists(dpath) >>> os.rmdir(dpath)
-
class
ubelt.util_path.
TempDir
[source]¶ Bases:
object
Context for creating and cleaning up temporary directories.
Example
>>> with TempDir() as self: >>> dpath = self.dpath >>> assert exists(dpath) >>> assert not exists(dpath)
Example
>>> self = TempDir() >>> dpath = self.ensure() >>> assert exists(dpath) >>> self.cleanup() >>> assert not exists(dpath)