ubelt.util_path module

ubelt.util_path.augpath(path, suffix='', prefix='', ext=None, base=None)[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 (str) – string representation of a path
  • suffix (str) – placed in front of the basename
  • prefix (str) – placed between the basename and trailing extension
  • ext (str) – if specified, replaces the trailing extension
  • base (str) – if specified, replaces the basename (without extension)
Returns:

newpath

Return type:

str

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'
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:str

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 (str) – 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:

str

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.truepath(path, real=False)[source]

Normalizes a string representation of a path and does shell-like expansion.

Parameters:
  • path (str) – string representation of a path
  • real (bool) – if True, all symbolic links are followed. (default: False)
Returns:

normalized path

Return type:

str

Note

This function is simlar 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 (str) – 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:

str

Notes

This function is not threadsafe 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)
ensure()[source]
cleanup()[source]
start()[source]