# How do I check whether a file exists without exceptions?

**URL:** https://sentry.io/answers/how-do-i-check-whether-a-file-exists-without-exceptions/

---

## The Problem

It is common practice to raise exceptions, especially when handling files. This practice is useful as it allows you to anticipate any errors that may occur during normal code execution instead of ignoring them.

You can also provide the user with useful feedback in case something goes wrong.

When working with files, it is useful to check if a file exists, and handle exceptions before you decide to do anything to it such as deleting or editing it.

A typical example of doing this is:

```python
my_file = "path/to/file"
try:
    f = open(my_file, "w")
except OSError:
    print("No such file: " + my_file)
```

On the other hand, sometimes you want to write a simple file-processing program without worrying about catching all exceptions. In such cases, how can you check whether a file exists without using exceptions?

## The Solution

There are several ways to check whether a file exists without using exceptions.

We're going to consider a couple of possible alternatives: using the [os.path](https://docs.python.org/3/library/os.path.html) or [pathlib](https://docs.python.org/3/library/pathlib.html) modules.

### Using `os.path`

You can use the `os.path.exists()` method:

```python
import os

os.path.exists("path/to/file")
```

The above method returns either `True` or `False` depending on whether the file exists.

### Using `pathlib`

The `pathlib` module could also be used:

```python
from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
    print("It lives!")
```

`is_file()` returns `True` or `False` depending on whether the file exists.

This module is an [Object-Oriented](https://realpython.com/python3-object-oriented-programming/) approach to file processing where you instantiate a `Path` object and pass it the file name. This approach is useful because it provides access to different file processing functionality in one place, which is an easier and more readable alternative to the standard `os.path` library.

Furthermore, `pathlib` "offers classes representing filesystem paths with semantics appropriate for different operating systems" ([Python Docs](https://docs.python.org/3/library/pathlib.html#module-pathlib)).

While the `pathlib` module is newer, it doesn't necessarily mean that it's better than using `os`. According to the [Python Docs](https://docs.python.org/3/library/os.path.html), "the `os.path` module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths". As a result, you would use it if you're writing programs that perform low-level string manipulation. [This article](https://betterprogramming.pub/should-you-be-using-pathlib-6f3a0fddec7e) provides a comparative analysis of when you would use either module.

---

*Source: [sentry.io/answers/how-do-i-check-whether-a-file-exists-without-exceptions/](https://sentry.io/answers/how-do-i-check-whether-a-file-exists-without-exceptions/)*
