In this post, we are going to learn about How to check if a file exists in Python using the OS module because it provides functions to work with the operating system that can be directory or files.
1. os.path.isfile() to check file exist in Python
The Python module isfile() method is a check that a file exists at the given path. It returns boolean values True if the file exists else returns False.
Syntax
os.path.isfile(filepath)
Parameter
- filepath : The path of file we want to check if exist.
Python Program to Check file exists in Python
In this example, we are passing the file path as an argument to isfile() method. If it returns TRUE that means the file exists at the specified path else the file does not exist at the given path. Let us understand with an Example.
#Python 3 Program to check file exist
import os.path
from os import path
if (path.os.path.isfile('./devenum.txt')) :
print ("File exists")
else:
print ("File not exists")
Output
File exists
2. os.path.exists() to check file exist in Python
The python os module exists() method allows us to find out if a directory or file exists at a specific path.
Syntax
os.path.exists(path)
Parameters
- path : It is required parameter we can pass file or directory path to this method as an argument.
Python Program to Check file exists in Python
In this example, we are passing the file path as an argument to os.path.exists() method.If it returns TRUE that means the file exists at the specified path and prints “DEVENUM File exists” else it returns False.
steps to check directory exists using os. path.exists()
- First import the os. path module using import os.path.
- check file or directory exists using the os module exists() method.
- The exists() method returns TRUE if a file or directory exists else return FALSE.
#Python Program to check file exists using exists() Method
import os.path
from os import path
if (path.exists('./devenum.txt')) :
print ("DEVENUM File exists")
else:
print ("DEVENUM File not exists")
Output
DEVENUM File exists
3. Using pathlib Module
In Python version 3.4+ pathlib module is used to handle with file system path. Let us check from example how to use this to find a file that exists at the given path.
Program Example
#Python Program to check file exists using pathlib exists() Method
import pathlib
myfile = pathlib.Path("./devenum.txt")
if myfile.exists ():
print ("File exist")
else:
print ("File not exist")
Output
File exist
4. glob Module to check file exist in Python
In this example, we are using the glob module glob() method to check file’s exist. It returns boolean values True and False. It returns true if the file exists and false when the file does not exist.
The file exists in the current directory, So we have to use the file name only instead of the full path.
Python Program Example
import glob
if glob.glob("geek.txt"):
print ("File exist")
else:
print("File does not exist")
Output
File exist
5. try/catch block to check ffile exist in Python
In this example, we are using the try/catch block to check the file exists. We have opened the file by using with statement. If a file exists then print “File exists” else throw an error then exceptional block execute.
Python Program Example
try:
with open('geek2.txt') as file:
print("File exist")
except IOError :
print("file does not exist")
Output
file does not exist
Summary
In this post, we have learned how to check files exist in Python with examples with the help of OS, Pathlib module, glob module, and try/except block.