Python is a scripting language and is highly readable, interactive, high-level, and object-oriented. Python has fewer syntactic structures than other programming
languages and uses English terms instead of punctuation. Python’s key features are: While performing operations on files like writing or
reading from a file, we need to first check if the file on which we are performing the particular operation exists or not. We use the is_file[] function, which is part of the Path class from the pathlib module, or exists[] function, which is part of the os.path module, in order to check if a file exists or not in Python. We will go in-depth on these functions in the coming topics. The OS module in
Python lets us interact with the operating system. This comes under Python’s standard utility modules and gives a portable way to use the dependent functions of the operating system. The exists[] function in Python exists in the os.path module, which is a submodule of the python’s OS module and is used to check if a particular file exists or not. from os.path import exists file_exists = exists[path_to_file] There are multiple ways to check if a file or directory exists or not. In this current article, we are going to perform this check using
Python functions. Some of them are explained briefly below- As mentioned in an earlier paragraph, we know that we use os.path.exists[] to check if a file or directory exists using Python. We further use this method to check if a particular file path refers to an
already open descriptor or not. os.path.exists[path] Return type - Return a boolean type value, i.e., false if the path does not exist and true if it exists. path - A string type or bytes type object referring to a system path. import os # Specifying path path = '/usr/local/bin/' # Checking whether the specified path exists isExisting = os.path.exists[path] print[isExisting] #
Specifying path path = '/home/User/Diksha/Desktop/file.txt' # Checking whether the specified path exists isExisting = os.path.exists[path] print[isExisting]Explain Python exists[]
Syntax
Different Ways to Verify a File or Python Check if Directory Exists, Using Functions
How to Check if a File Exists in Python Using:
1. os.path.exists[]
False
True
2. os.path.isfile[]
os.path.isfile[] is another method that is used to check if a particular regular file in Python exists or not.
Function Syntaxos.path.isfile[path]
ParameterReturn type - Return a boolean type value.
path - A string type or bytes type object referring to a system path.
Exampleimport os
# defining Path
path = 'C:/Users/simplilearn/Desktop/file.txt'
# Checking whether the specified path exists
isFile = os.path.isfile[path]
print[isFile]
# defining Path
path = '/home/User/Diksha/Desktop/'
# Checking whether the specified path exists
isFile = os.path.isfile[path]
print[isFile]
OutputTrue
False
3. os.path.isdir[]
This function is used to check if a specified path exists in a directory or not. The specified path follows a symbolic path linking to the specified directory.
Function Syntaxos.path.isdir[path]
ParameterReturn type - Returns a boolean type value, i.e., false if the specified path to the existing does not exist, true if it does.
path - os.path.isdir[path]
import os.path
# defining Path
path = '/home/User/Documents/file.txt'
# Check whether the specified path exists
isdir = os.path.isdir[path]
print[isdir]
# defining Path
path = '/home/User/Documents/'
# Check whether the specified path exists
isdir = os.path.isdir[path]
print[isdir]
OutputTrue
False
Example 2 - To Check if the Specified Path Is a Symbolic Pathimport os.path
# Creating a directory
dirname = "Simplilearn"
os.mkdir[dirname]
# Creating a symbolic link to point to above directory
symbolink_path = "/home/User/Desktop/simp"
os.symlink[dirname, symbolink_path]
path = dirname
# Checking if
# specified path is an existing directory
isdir = os.path.isdir[path]
print[isdir]
path = symbolink_path
# Checking whether the specified path
# is an existing directory
isdir = os.path.isdir[path]
print[isdir]
OutputTrue
True
4. pathlibPath.exists[]
The Python Pathlib module contains a number of classes that describe file system paths and have semantics that are acceptable for various operating systems. This module is part of Python's standard utility module collection. Concrete and Pure paths are the two types of path classes in the pathlib module. Pure routes only perform computations and do not perform I/O operations, whereas concrete pathways are inherited from pure paths and perform both I/O and computational activities.
The pathlib.Path.exists[] method is mainly used to check if the given path points to an existing directory or a file.
Function Syntaxpathlib.Path.exists[path]
ParameterReturn type - Return a boolean type value, true if the path exists, false if it does not.
path - Object that represents the system path.
Examplefrom pathlib import Path
# defining Path
path = '/home/simplilearn/Desktop'
# Instantiating the Path class
obj = Path[path]
# Checking if the path points to and existing file
print[obj.exists[]]
OutputTrue
Learn the essentials of object-oriented programming, web development with Django, and more with the Python Training Course. Enroll now!
Get Python Certified With Simplilearn
In this article, we discussed the various ways to check if a file or directory exists or not using Python. Before performing any operations on the files or directory using built-in Python functions, it is extremely crucial to check if a particular file or directory exists or not in Python.
To get deeper knowledge in file systems in Python, going through a thorough, comprehensive course of Python is a considerable choice. To make things easy and get on board to jumpstart your career as a Mobile and Software developer in Python, Simplilearn provides complete Python development training. Check it out to get started with your Python journey!