Summary: In this tutorial, you will learn how to check if a file exists.
When processing files, you’ll often want to check if a file exists before doing anything else with it, such as reading the file or writing to it.
To do this, you can use the exists() function of the os.path module or the is_file() method of the Path class in the pathlib module.
The os.path.exists() function in os.path import exists file_exists = exists(path_to_file)Code language: JavaScript (javascript)
Path.is_file
() Pathlib import method Path path = Path(path_to_file) path.is_file()Code language: JavaScript (javascript)1) Using the os.path.exists() function to check if a file exists
To check if a file exists, Pass the file path to the exists() function from the
OS.Path
standard library.
First, import the standard library
os.path:import os.pathCode language: JavaScript (javascript)Second, call the exists():os.path.exists(path_to_file)function Code language: CSS (css)If the file exists
, the exists() function
returns True. Otherwise, it returns False.
If the file is
in the same folder as the program
, the path_to_file is simply the name of the file. However,
this is not the case, you must pass the full path of the file. For example:
/path/to/filename
Even if you run the program in Windows, you must use the forward slash (/) to separate the path. It will work on Windows, macOS and Linux.
The following example uses the exists() function to check whether the readme.txt file exists in the same folder as the program:import os.path file file_exists = os.path.exists(‘readme.txt’) print(file_exists)Code language: JavaScript (javascript)
If the readme.txt file exists
, you will see the following output:TrueCode language: PHP
(php)
Otherwise, you will see False on the screen
: FalseCode language: PHP (php)To make the call to the exists() function
shorter and more obvious, you can import that function and rename it to file_exists() function like this
:from os.path import exists as file_exists file_exists(‘readme.txt’)Code language: JavaScript (javascript)2) Using the pathlib module
to check if a file exists, Python introduced the pathlib module
from version 3.4.
The pathlib module allows you to manipulate files and folders using the object-oriented approach. If you are not familiar with object-oriented programming, see the Python OOP section.
First, import the Path class from the pathlib module:from
pathlib
import PathCode language: JavaScript (javascript)Next, create an instance of
a new instance of the Path class and initialize it with the path of the file you want to check for existence
:p ath = Path(path_to_file)
Finally, check if the file exists using the is_file
() method: path.is_file()Code language: CSS (css)If the
file does not exist, the is_file() method returns False. Otherwise, returns True.
The
following example shows how to use the Path class of the pathlib module to check if the readme.txt file exists in the same program folder:from pathlib
import Path path_to_file = ‘readme.txt’ path = Path(path_to_file) if path.is_file(): print(f’File {path_to_file} exists’) else: print(f’File {path_to_file} does not exist’)Code language: PHP (php)If the readme.txt file exists
, you will see the following output:The readme file.txt
existsCode language: CSS (css)
Summary
- Use the os.path.exists() function or the Path.is_file() method to check if a file exists