pathutils - Filesystem fun

Functions for working with filesystem paths.

The expandpath() function expands the tilde to $HOME and environment variables to their values.

The augpath() function creates variants of an existing path without having to spend multiple lines of code splitting it up and stitching it back together.

The shrinkuser() function replaces your home directory with a tilde.

boltons.pathutils.augpath(path, suffix='', prefix='', ext=None, base=None, dpath=None, multidot=False)[source]

Augment a path by modifying its components.

Creates a new path with a different extension, basename, directory, 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. Essentially a path is broken down into components (dpath, base, ext), and then recombined as (dpath, prefix, base, suffix, ext) after replacing any specified component.

  • path (str | PathLike) – a path to augment
  • suffix (str, default='') – placed between the basename and extension
  • prefix (str, default='') – placed in front of the basename
  • ext (str, default=None) – if specified, replaces the extension
  • base (str, default=None) – if specified, replaces the basename without extension
  • dpath (str | PathLike, default=None) – if specified, replaces the directory
  • multidot (bool, default=False) – Allows extensions to contain multiple dots. Specifically, 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.

augmented path

Return type:



>>> path = ''
>>> suffix = '_suff'
>>> prefix = 'pref_'
>>> ext = '.baz'
>>> newpath = augpath(path, suffix, prefix, ext=ext, base='bar')
>>> print('newpath = %s' % (newpath,))
newpath = pref_bar_suff.baz


>>> augpath('')
>>> augpath('', ext='.BAZ')
>>> augpath('', suffix='_')
>>> augpath('', prefix='_')
>>> augpath('', base='baz')
>>> augpath('foo.tar.gz', ext='.zip', multidot=True)
>>> augpath('foo.tar.gz', ext='.zip', multidot=False)
>>> augpath('foo.tar.gz', suffix='_new', multidot=True)
boltons.pathutils.shrinkuser(path, home='~')[source]

Inverse of os.path.expanduser().

  • path (str | PathLike) – path in system file structure
  • home (str, default='~') – symbol used to replace the home path. Defaults to ‘~’, but you might want to use ‘$HOME’ or ‘%USERPROFILE%’ instead.

path: shortened path replacing the home directory with a tilde

Return type:



>>> path = expanduser('~')
>>> assert path != '~'
>>> assert shrinkuser(path) == '~'
>>> assert shrinkuser(path + '1') == path + '1'
>>> assert shrinkuser(path + '/1') == join('~', '1')
>>> assert shrinkuser(path + '/1', '$HOME') == join('$HOME', '1')

Shell-like expansion of environment variables and tilde home directory.

Parameters:path (str | PathLike) – the path to expand
Returns:expanded path
Return type:str


>>> import os
>>> os.environ['SPAM'] = 'eggs'
>>> assert expandpath('~/$SPAM') == expanduser('~/eggs')
>>> assert expandpath('foo') == 'foo'