vistir.path module


Checks to see if a pathlib Path object is a unc path or not.

vistir.path.get_converted_relative_path(path, relative_to=None)[source]

Convert path to be relative.

Given a vague relative path, return the path relative to the given location.

  • path (str) – The location of a target path
  • relative_to (str) – The starting path to build against, optional

A relative posix-style path with a leading ./

This performs additional conversion to ensure the result is of POSIX form, and starts with ./, or is precisely ..

>>> os.chdir('/home/user/code/myrepo/myfolder')
>>> vistir.path.get_converted_relative_path('/home/user/code/')
>>> vistir.path.get_converted_relative_path('/home/user/code/myrepo/myfolder/mysubfolder')
>>> vistir.path.get_converted_relative_path('/home/user/code/myrepo/myfolder')
vistir.path.handle_remove_readonly(func, path, exc)[source]

Error handler for shutil.rmtree.

Windows source repo folders are read-only by default, so this error handler attempts to set them as writeable and then proceed with deletion.

  • func (function) – The caller function
  • path (str) – The target path for removal
  • exc (Exception) – The raised exception

This function will call check is_readonly_path() before attempting to call set_write_bit() on the target path and try again.


Return a case-normalized absolute variable-expanded path.

Parameters:path (str) – The non-normalized path
Returns:A normalized, expanded, case-normalized path
Return type:str
vistir.path.is_in_path(path, parent)[source]

Determine if the provided full path is in the given parent root.

  • path (str) – The full path to check the location of.
  • parent (str) – The parent path to check for membership in

Whether the full path is a member of the provided parent.

Return type:



Returns true if the given url is a file url.


Check if a provided path exists and is readonly.

Permissions check is bool(path.stat & stat.S_IREAD) or not os.access(path, os.W_OK)


Checks if a given string is an url.

vistir.path.mkdir_p(newdir, mode=511)[source]

Recursively creates the target directory and all of its parents if they do not already exist. Fails silently if they do.

Parameters:newdir (str) – The directory path to ensure
Raises:OSError if a file is encountered along the way

Decorator to ensure mkdir_p is called to the function’s return value.

vistir.path.create_tracked_tempdir(*args, **kwargs)[source]

Create a tracked temporary directory.

This uses TemporaryDirectory, but does not remove the directory when the return value goes out of scope, instead registers a handler to cleanup on program exit.

The return value is the path to the created directory.

vistir.path.create_tracked_tempfile(*args, **kwargs)[source]

Create a tracked temporary file.

This uses the NamedTemporaryFile construct, but does not remove the file until the interpreter exits.

The return value is the file object.


Convert the supplied local path to a file uri.

Parameters:path (str) – A string pointing to or representing a local path
Returns:A file:// uri for the same location
Return type:str
>>> path_to_url("/home/user/code/myrepo/")
vistir.path.rmtree(directory, ignore_errors=False, onerror=None)[source]

Stand-in for rmtree() with additional error-handling.

This version of rmtree handles read-only paths, especially in the case of index files written by certain source control systems.

  • directory (str) – The target directory to remove
  • ignore_errors (bool) – Whether to ignore errors, defaults to False
  • onerror (func) – An error handling function, defaults to handle_remove_readonly()


Setting ignore_errors=True may cause this to silently fail to delete the path


Call os.path.expandvars if value is a string, otherwise do nothing.


Set read-write permissions for the current user on the target path. Fail silently if the path doesn’t exist.

Parameters:fn (str) – The target filename or path

Convert a valid file url to a local filesystem path.

Follows logic taken from pip’s equivalent function


Mimic os.walk, but walk ‘up’ instead of down the directory tree.