---
name: "pathtools3"
section: "3"
platform: "linux"
package: "python3-pathtools"
version: "0.1.2-7"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/pathtools3"
---

# pathtools3(3)

pathtools Documentation Python API library for common path and pattern functionality.

## NAME

pathtools - pathtools Documentation

Python API library for common path and pattern functionality.

## EASY INSTALLATION

You can use *pip* to install **pathtools** quickly and easily:

```
$ pip install pathtools
```

## API REFERENCE

### *pathtools.path*

- **module**

  pathtools.path

- **synopsis**

  Directory walking, listing, and path sanitizing functions.

- **author**

  Yesudeep Mangalapilly \<*yesudeep@gmail.com*\>

### Functions

- **pathtools.path.get\_dir\_walker(recursive, topdown=True, followlinks=False)**

  Returns a recursive or a non-recursive directory walker.

  - **Parameters**

    **recursive** -- **True** produces a recursive walker; **False** produces a non-recursive walker.

  - **Returns**

    A walker function.

- **pathtools.path.walk(dir\_pathname, recursive=True, topdown=True, followlinks=False)**

  Walks a directory tree optionally recursively. Works exactly like **os.walk()** only adding the *recursive* argument.

  - **Parameters**

    - •

      **dir\_pathname** -- The directory to traverse.

    - •

      **recursive** -- **True** for walking recursively through the directory tree; **False** otherwise.

    - •

      **topdown** -- Please see the documentation for **os.walk()**

    - •

      **followlinks** -- Please see the documentation for **os.walk()**

- **pathtools.path.listdir(dir\_pathname, recursive=True, topdown=True, followlinks=False)**

  Enlists all items using their absolute paths in a directory, optionally recursively.

  - **Parameters**

    - •

      **dir\_pathname** -- The directory to traverse.

    - •

      **recursive** -- **True** for walking recursively through the directory tree; **False** otherwise.

    - •

      **topdown** -- Please see the documentation for **os.walk()**

    - •

      **followlinks** -- Please see the documentation for **os.walk()**

- **pathtools.path.list\_directories(dir\_pathname, recursive=True, topdown=True, followlinks=False)**

  Enlists all the directories using their absolute paths within the specified directory, optionally recursively.

  - **Parameters**

    - •

      **dir\_pathname** -- The directory to traverse.

    - •

      **recursive** -- **True** for walking recursively through the directory tree; **False** otherwise.

    - •

      **topdown** -- Please see the documentation for **os.walk()**

    - •

      **followlinks** -- Please see the documentation for **os.walk()**

- **pathtools.path.list\_files(dir\_pathname, recursive=True, topdown=True, followlinks=False)**

  Enlists all the files using their absolute paths within the specified directory, optionally recursively.

  - **Parameters**

    - •

      **dir\_pathname** -- The directory to traverse.

    - •

      **recursive** -- **True** for walking recursively through the directory tree; **False** otherwise.

    - •

      **topdown** -- Please see the documentation for **os.walk()**

    - •

      **followlinks** -- Please see the documentation for **os.walk()**

- **pathtools.path.absolute\_path(path)**

  Returns the absolute path for the given path and normalizes the path.

  - **Parameters**

    **path** -- Path for which the absolute normalized path will be found.

  - **Returns**

    Absolute normalized path.

- **pathtools.path.real\_absolute\_path(path)**

  Returns the real absolute normalized path for the given path.

  - **Parameters**

    **path** -- Path for which the real absolute normalized path will be found.

  - **Returns**

    Real absolute normalized path.

- **pathtools.path.parent\_dir\_path(path)**

  Returns the parent directory path.

  - **Parameters**

    **path** -- Path for which the parent directory will be obtained.

  - **Returns**

    Parent directory path.

### *pathtools.patterns*

- **module**

  pathtools.patterns

- **synopsis**

  Wildcard pattern matching and filtering functions for paths.

- **author**

  Yesudeep Mangalapilly \<*yesudeep@gmail.com*\>

### Functions

- **pathtools.patterns.match\_path(pathname, included\_patterns=None, excluded\_patterns=None, case\_sensitive=True)**

  Matches a pathname against a set of acceptable and ignored patterns.

  - **Parameters**

    - •

      **pathname** -- A pathname which will be matched against a pattern.

    - •

      **included\_patterns** -- Allow filenames matching wildcard patterns specified in this list. If no pattern is specified, the function treats the pathname as a match\_path.

    - •

      **excluded\_patterns** -- Ignores filenames matching wildcard patterns specified in this list. If no pattern is specified, the function treats the pathname as a match\_path.

    - •

      **case\_sensitive** -- **True** if matching should be case-sensitive; **False** otherwise.

  - **Returns**

    **True** if the pathname matches; **False** otherwise.

  - **Raises**

    ValueError if included patterns and excluded patterns contain the same pattern.

  - **Doctests::**

    ```
    >>> match_path("/Users/gorakhargosh/foobar.py")
    True
    >>> match_path("/Users/gorakhargosh/foobar.py", case_sensitive=False)
    True
    >>> match_path("/users/gorakhargosh/foobar.py", ["*.py"], ["*.PY"], True)
    True
    >>> match_path("/users/gorakhargosh/FOOBAR.PY", ["*.py"], ["*.PY"], True)
    False
    >>> match_path("/users/gorakhargosh/foobar/", ["*.py"], ["*.txt"], False)
    False
    >>> match_path("/users/gorakhargosh/FOOBAR.PY", ["*.py"], ["*.PY"], False)
    Traceback (most recent call last):
        ...
    ValueError: conflicting patterns `set(['*.py'])` included and excluded
    ```

- **pathtools.patterns.match\_path\_against(pathname, patterns, case\_sensitive=True)**

  Determines whether the pathname matches any of the given wildcard patterns, optionally ignoring the case of the pathname and patterns.

  - **Parameters**

    - •

      **pathname** -- A path name that will be matched against a wildcard pattern.

    - •

      **patterns** -- A list of wildcard patterns to match\_path the filename against.

    - •

      **case\_sensitive** -- **True** if the matching should be case-sensitive; **False** otherwise.

  - **Returns**

    **True** if the pattern matches; **False** otherwise.

  - **Doctests::**

    ```
    >>> match_path_against("/home/username/foobar/blah.py", ["*.py", "*.txt"], False)
    True
    >>> match_path_against("/home/username/foobar/blah.py", ["*.PY", "*.txt"], True)
    False
    >>> match_path_against("/home/username/foobar/blah.py", ["*.PY", "*.txt"], False)
    True
    >>> match_path_against("C:\windows\blah\BLAH.PY", ["*.py", "*.txt"], True)
    False
    >>> match_path_against("C:\windows\blah\BLAH.PY", ["*.py", "*.txt"], False)
    True
    ```

- **pathtools.patterns.filter\_paths(pathnames, included\_patterns=None, excluded\_patterns=None, case\_sensitive=True)**

  Filters from a set of paths based on acceptable patterns and ignorable patterns.

  - **Parameters**

    - •

      **pathnames** -- A list of path names that will be filtered based on matching and ignored patterns.

    - •

      **included\_patterns** -- Allow filenames matching wildcard patterns specified in this list. If no pattern list is specified, \["\*"\] is used as the default pattern, which matches all files.

    - •

      **excluded\_patterns** -- Ignores filenames matching wildcard patterns specified in this list. If no pattern list is specified, no files are ignored.

    - •

      **case\_sensitive** -- **True** if matching should be case-sensitive; **False** otherwise.

  - **Returns**

    A list of pathnames that matched the allowable patterns and passed through the ignored patterns.

  - **Doctests::**

    ```
    >>> pathnames = set(["/users/gorakhargosh/foobar.py", "/var/cache/pdnsd.status", "/etc/pdnsd.conf", "/usr/local/bin/python"])
    >>> set(filter_paths(pathnames)) == pathnames
    True
    >>> set(filter_paths(pathnames, case_sensitive=False)) == pathnames
    True
    >>> set(filter_paths(pathnames, ["*.py", "*.conf"], ["*.status"], case_sensitive=True)) == set(["/users/gorakhargosh/foobar.py", "/etc/pdnsd.conf"])
    True
    ```

Found a bug in or want a feature added to **pathtools**? You can fork the official *code repository* or file an issue ticket at the *issue tracker*.

- •

  *Index*

- •

  *Module Index*

- •

  *Search Page*

## AUTHOR

Yesudeep Mangalapilly

## COPYRIGHT

2010, Yesudeep Mangalapilly
