> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# pathlib.Path Read

#### Sourcery refactoring id: `path-read`

#### Description:

Simplify basic file reads with `pathlib`.

#### Before:

```python
with open("file.txt") as f:
    file_contents = f.read()
```

#### After:

```python

file_contents = pathlib.Path("file.txt").read_text()
```

#### Explanation:

`pathlib` is Python's built-in object-oriented file system path API. `Path`s are
more versatile than strings and have cross-platform support.

The
[`Path.read_text`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.read_text)
method opens the file, reads it in text-mode and returns its contents making
sure it gets closed. By using this method, we save an indentation level and
remove the need for manually using a context manager.

This refactoring also ensures that `pathlib` gets imported in case it is not
already available.
