Python How to Check If File Exists

Python how to check if a file exists
Python how to check if a file exists
Check if a file exists using Python. Image design by Elias Ervast

To check if a file exists using Python, use the exists() function from the os.path module:

from os.path import exists

if exists(path_to_file):
    # Do something

Here is a short illustration of checking if a file exists in the same folder:

Alternatively, you can use the is_file() function from the Path class from the pathlib module:

from pathlib import Path

path = Path(path_to_file)

if path.is_file():
    # Do something

Where path_to_file is relative to the Python file that checks the existence of the file.

For example, if there’s a file called hello.txt in the same folder as this Python program, the path_to_file is the name of the file.

More Detailed Steps to Check If a File Exists in Python

Before you start working with a file, you may want to check if one exists. To do this you can use the exists() function from os.path module.

The os.path module is is useful when processing files from different places in the file system of your program.

Make sure you import the os.path module before trying to use it:

import os.path

Then, call the exists() function:

os.path.exists(path_to_file)

Where path_to_file is the path to the file you are seeking (relative to the Python file’s path). If the file is in the same folder as your Python program, the path is just the name of the file.

If the file exists, the exists() function returns True. Otherwise, it returns False.

The following example program uses the exists() function to check if the hello.txt file exists:

import os.path

file_found = os.path.exists('hello.txt')

print(file_found)

If the hello.txt file exists, you’ll see the following output:

True

To make the call to the exists() function shorter, import the function from the module:

from os.path import exists

exists('hello.txt')

And here is an example where the file is in another folder called some_files:

Here you need to provide the path to the file to find out if it exists. Because the folder where the file is, is in the same folder as the Python program, its path is:

some_files/hello.txt

Use the Pathlib Module to Check If a File Exists

The pathlib module lets you manipulate files and folders.

To use it, import the Path class from the pathlib module:

from pathlib import Path

Then, create a Path object by initializing it with the file path whose existence is the point of interest:

path = Path(path_to_file)

Now, check if the file exists using the is_file() method. This essentially tests if the object you created is a valid file.

path.is_file()

For instance, let’s use this approach to see if the hello.txt file exists:

from pathlib import Path

path_to_file = 'hello.txt'
path = Path(path_to_file)

if path.is_file():
    print(f'{path_to_file} exists')
else:
    print(f'{path_to_file} does not exist')

If the hello.txt file exists, you’ll see the following output:

hello.txt exists

Conclusion

Thanks for reading. I hope you enjoy it.

Happy coding.

Further Reading

50 Python Interview Questions with Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *