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)

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.

参数:path (str) – A PyFilesytem path.
返回:An absolute path.
返回类型:str
fs.path.basename(path)

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).

参数:path (str) – A PyFilesytem path.
返回类型:str
>>> basename('foo/bar/baz')
'baz'
>>> basename('foo/bar')
'bar'
>>> basename('foo/bar/')
''
fs.path.combine(path1, path2)

Join two paths together.

参数:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
返回类型:

str

This is faster than pathjoin, 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)

Return the parent directory of a path.

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

参数:path (str) – A PyFilesytem path.
返回类型:str
>>> dirname('foo/bar/baz')
'foo/bar'
>>> dirname('/foo/bar')
'/foo'
>>> dirname('/foo')
'/'
fs.path.forcedir(path)

Ensure the path ends with a trailing forward slash

参数:path – A PyFilesytem path.
返回类型:bool
>>> forcedir("foo/bar")
'foo/bar/'
>>> forcedir("foo/bar/")
'foo/bar/'
fs.path.frombase(path1, path2)

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

参数:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
返回类型:

str

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

Check if a path is an absolute path.

参数:path (str) – A PyFilesytem path.
返回类型:bool
fs.path.isbase(path1, path2)

Check if path1 is a base of path2.

参数:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
返回类型:

bool

fs.path.isdotfile(path)

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

参数:path (str) – Path to check.
返回类型:bool
>>> isdotfile('.baz')
True
>>> isdotfile('foo/bar/.baz')
True
>>> isdotfile('foo/bar.baz')
False
fs.path.isparent(path1, path2)

Check if path1 is a parent directory of path2.

参数:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
返回类型:

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)

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

参数:
  • path1 (str) – A PyFilesytem path.
  • path2 (str) – A PyFilesytem path.
返回类型:

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)

Check if a path ends with a wildcard.

参数:path (int) – An FS path.
返回类型:bool
>>> iswildcard('foo/bar/baz.*')
True
>>> iswildcard('foo/bar')
False
fs.path.iteratepath(path)

Iterate over the individual components of a path.

>>> iteratepath('/foo/bar/baz')
['foo', 'bar', 'baz']
参数:path (str) – Path to iterate over.
返回:A list of path components.
返回类型:list
fs.path.join(*paths)

Join any number of paths together.

参数:paths – Paths to join are given in positional arguments.
返回类型:str
>>> pathjoin('foo', 'bar', 'baz')
'foo/bar/baz'
>>> pathjoin('foo/bar', '../baz')
'foo/baz'
>>> pathjoin('foo/bar', '/baz')
'/baz'
fs.path.normpath(path)

Normalize a path.

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

参数:path (str) – Path to normalize.
返回: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)

Get intermediate paths from the root to the given path.

参数:
  • path (str) – A PyFilesystem path
  • reverse (bool) – Reverses the order of the paths.
返回:

A list of paths.

返回类型:

list

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

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

参数:
  • 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)

Convert the given path to a relative path.

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

参数:path (str) – Path to adjust
返回类型:str
>>> relpath('/a/b')
'a/b'
fs.path.split(path)

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.

参数:path (str) – Path to split
返回:tuple of (head, tail)
返回类型:tuple
>>> split("foo/bar")
('foo', 'bar')
>>> split("foo/bar/baz")
('foo/bar', 'baz')
>>> split("/foo/bar/baz")
('/foo/bar', 'baz')
fs.path.splitext(path)

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

参数:path – A path to split
返回:tuple of (path, extension)
返回类型:tuple
>>> splitext('baz.txt')
('baz', '.txt')
>>> splitext('foo/bar/baz.txt')
('foo/bar/baz', '.txt')