Checking if a File Exists in Python

You can check if a file exists in Python using the path.exists() method, which is included in the built-in OS module. There are two methods in the OS module that are used to check for the existence of a file: the path.isfile() and path.exists() methods. The path.isfile() method takes a file path as an argument and a boolean value on whether the file exists. The path.exists() method checks if the specified path exists. Like the path.isfile() methods, it takes a path as an argument and returns a boolean value. You can also check if the specified path is a directory using the path.isdir() method. The path.isdir() method takes a path as an argument and returns a boolean value (True/False) depending on whether the path points to a directory or not. In this Python Check if File Exists example, we use the path.exists() method to check if the required files exist in the specified directory. Click Execute to run Python Check if File Exists Example online and see the result.
Checking if a File Exists in Python Execute
import os
 
my_file = "filename.txt"
 
if os.path.exists(my_file):
  print(f'The file {my_file} exists')
else:
  print(f'The file {my_file} does not exist')
  
  
Updated: Viewed: 1718 times

What is Python

The Python programming language is one of the most popular general-purpose programming languages. Python is used for server-side programming, machine learning, application testing, and as "glue code" to connect different components from different applications written in different programming languages. Python has dynamic typing and dynamic binding, and an integrated garbage collector.The Python programming language runs on almost all operating systems, such as Windows, Linux, and macOS. Python is a straightforward programming language to learn and maintain. Python has built-in modules for working with JSON, strings, HTTP requests, and XML out of the box.

What is OS Module in Python?

In Python, the OS Module provides various methods to interact with the operating system. The OS Module is included in standard Python utility modules. The OS module provides a portable way to use operating system-specific features. The OS Module hides the difference for a Python programmer between different operating systems. In addition to methods for checking the existence of files on disk, the OS module contains an extensive set of useful functions that are used to interact with the computer's operating system. To use the os module, it must first be imported so that we can call methods to interact with the file and the operating system:

Python OS Module Example
import os

Check if a file exists in Python using os.path.isfile() Method

To check if a file exists in Python, you can use the os.path.isfile() method. Pass a file name and a relative or absolute path to the file as an argument to this function. The method returns True if the file is present, and False otherwise.

Python os.path.isfile() Method Example
import os.path

my_file = "filename.txt"

result = os.path.isfile(my_file)

if result:
    print(f'The file {my_file} exists')
else:
    print(f'The file {my_file} does not exist')

# The file filename.txt does not exist

Check if a file exists in Python using os.path.exists() Method

To check if a file exists in Python, you can use the os.path.exists() method. The method returns True if the path is a regular file, directory, or a valid symbolic link.

Python os.path.exists() Method Example
import os

file_path = r'C:/homework/my_file.txt'

result = os.path.exists(file_path)
if result:
    print(f'The file {file_path} exists')
else:
    print(f'The file {file_path} does not exist')

# The file C:/homework/my_file.txt does not exist

How to check if a directory exists in Python?

To check if a directory exists in Python, you can use the os.path.isdir() method. The os.path.isdir() method returns True if the path is a directory or a symbolic link to a directory:

Python os.path.isdir() Method Example
import os

if os.path.isdir(r'C:\homework\files'):
    print("Directory exist")
else:
    print("Directory not exist")

# Directory not exist

See also