fs.path

Useful functions for working with PyFilesystem paths. sdsd This is broadly similar to the standard os.path module but works with paths in the canonical format expected by all FS objects (that is, separated by forward slashes and with an optional leading slash).

See Paths for an explanation of PyFilesystem paths.

fs.path.abspath(path)[source]

Convert the given path to an absolute path.

Since FS objects have no concept of a current directory, this simply adds a leading / character if the path doesn’t already have one.

Parameters:path (str) – A PyFilesytem path.
Returns:An absolute path.
Return type:str
fs.path.basename(path)[source]

Return the basename of the resource referenced by a path.

This is always equivalent to the ‘tail’ component of the value returned by split(path).

Parameters:path (str) – A PyFilesytem path.
Return type:str
>>> basename('foo/bar/baz')
'baz'
>>> basename('foo/bar')
'bar'
>>> basename('foo/bar/')
''
fs.path.combine(path1, path2)[source]

Join two paths together.

Parameters:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
Return type:

str

This is faster than join(), but only works when the second path is relative, and there are no back references in either path.

>>> combine("foo/bar", "baz")
'foo/bar/baz'
fs.path.dirname(path)[source]

Return the parent directory of a path.

This is always equivalent to the ‘head’ component of the value returned by split(path).

Parameters:path (str) – A PyFilesytem path.
Return type:str
>>> dirname('foo/bar/baz')
'foo/bar'
>>> dirname('/foo/bar')
'/foo'
>>> dirname('/foo')
'/'
fs.path.forcedir(path)[source]

Ensure the path ends with a trailing forward slash

Parameters:path – A PyFilesytem path.
Return type:bool
>>> forcedir("foo/bar")
'foo/bar/'
>>> forcedir("foo/bar/")
'foo/bar/'
fs.path.frombase(path1, path2)[source]

Get the final path of path2 that isn’t in path1.

Parameters:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
Return type:

str

>>> frombase('foo/bar/', 'foo/bar/baz/egg')
'baz/egg'
fs.path.isabs(path)[source]

Check if a path is an absolute path.

Parameters:path (str) – A PyFilesytem path.
Return type:bool
fs.path.isbase(path1, path2)[source]

Check if path1 is a base of path2.

Parameters:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
Return type:

bool

fs.path.isdotfile(path)[source]

Detect if a path references a dot file, i.e. a resource who’s name starts with a ‘.’

Parameters:path (str) – Path to check.
Return type:bool
>>> isdotfile('.baz')
True
>>> isdotfile('foo/bar/.baz')
True
>>> isdotfile('foo/bar.baz')
False
fs.path.isparent(path1, path2)[source]

Check if path1 is a parent directory of path2.

Parameters:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
Return type:

bool

>>> isparent("foo/bar", "foo/bar/spam.txt")
True
>>> isparent("foo/bar/", "foo/bar")
True
>>> isparent("foo/barry", "foo/baz/bar")
False
>>> isparent("foo/bar/baz/", "foo/baz/bar")
False
fs.path.issamedir(path1, path2)[source]

Check if two paths reference a resource in the same directory.

Parameters:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
Return type:

bool

>>> issamedir("foo/bar/baz.txt", "foo/bar/spam.txt")
True
>>> issamedir("foo/bar/baz/txt", "spam/eggs/spam.txt")
False
fs.path.iswildcard(path)[source]

Check if a path ends with a wildcard.

Parameters:path (int) – An FS path.
Return type:bool
>>> iswildcard('foo/bar/baz.*')
True
>>> iswildcard('foo/bar')
False
fs.path.iteratepath(path)[source]

Iterate over the individual components of a path.

>>> iteratepath('/foo/bar/baz')
['foo', 'bar', 'baz']
Parameters:path (str) – Path to iterate over.
Returns:A list of path components.
Return type:list
fs.path.join(*paths)[source]

Join any number of paths together.

Parameters:paths – Paths to join are given in positional arguments.
Return type:str
>>> join('foo', 'bar', 'baz')
'foo/bar/baz'
>>> join('foo/bar', '../baz')
'foo/baz'
>>> join('foo/bar', '/baz')
'/baz'
fs.path.normpath(path)[source]

Normalize a path.

This function simplifies a path by collapsing back-references and removing duplicated separators.

Parameters:path (str) – Path to normalize.
Returns:A valid FS path.
Type:str
>>> normpath("/foo//bar/frob/../baz")
'/foo/bar/baz'
>>> normpath("foo/../../bar")
Traceback (most recent call last)
    ...
IllegalBackReference: Too many backrefs in 'foo/../../bar'
fs.path.recursepath(path, reverse=False)[source]

Get intermediate paths from the root to the given path.

Parameters:
  • path (str) – A PyFilesystem path
  • reverse (bool) – Reverses the order of the paths.
Returns:

A list of paths.

Return type:

list

>>> recursepath('a/b/c')
['/', '/a', '/a/b', '/a/b/c']
fs.path.relativefrom(base, path)[source]

Return a path relative from a given base path, i.e. insert backrefs as appropriate to reach the path from the base.

Parameters:
  • base (str) – Path to a directory.
  • path (atr) – Path you wish to make relative.
>>> relativefrom("foo/bar", "baz/index.html")
'../../baz/index.html'
fs.path.relpath(path)[source]

Convert the given path to a relative path.

This is the inverse of abspath(), stripping a leading '/' from the path if it is present.

Parameters:path (str) – Path to adjust
Return type:str
>>> relpath('/a/b')
'a/b'
fs.path.split(path)[source]

Split a path into (head, tail) pair.

This function splits a path into a pair (head, tail) where ‘tail’ is the last pathname component and ‘head’ is all preceding components.

Parameters:path (str) – Path to split
Returns:tuple of (head, tail)
Return type:tuple
>>> split("foo/bar")
('foo', 'bar')
>>> split("foo/bar/baz")
('foo/bar', 'baz')
>>> split("/foo/bar/baz")
('/foo/bar', 'baz')
fs.path.splitext(path)[source]

Split the extension from the path, and returns the path (up to the last ‘.’ and the extension).

Parameters:path – A path to split
Returns:tuple of (path, extension)
Return type:tuple
>>> splitext('baz.txt')
('baz', '.txt')
>>> splitext('foo/bar/baz.txt')
('foo/bar/baz', '.txt')