Home » Python

Check if a file exists in Python

Checking file existence: Here, we are going to learn how to check whether a file exists or not in Python programing language?
Submitted by Sapna Deraje Radhakrishna, on October 03, 2019

An ability to check if the file exists or not, is very crucial in any application. Often, the applications perform verifications like,

  • Check if the file exists before appending/writing to it.
  • Check if the file exists before reading it.

The python programming language provides multiple methods to check if the file exists or not. The module which provides the functions as ‘os' , so it is important to import os, while there is a verification on file's existence.

os.path.exists()

-bash-4.2$ ls
python_samples  test.txt

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> from os import path
>>> print(path.exists('test.txt'))
True
>>> print(path.exists('test1.txt'))
False
>>>

os.path.isfile()

-bash-4.2$ ls
python_samples  test.txt

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> print(os.path.isfile('test.txt'))
True
>>> print(os.path.isfile('test1.txt'))
False

The functions, demonstrated above are also available in a lower version of python (< 3). However, python version 3.4 provides a function pathlibPath.exists() which is imported from pathlib module for handling the file system path. It uses an object-oriented approach to verify if the file exists or not.

-bash-4.2$ ls
python_samples  test.txt

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pathlib
>>> test_file = 'test.txt'
#create a file object
>>> file = pathlib.Path(test_file)
>>> if file.exists():
...     print("file {} exists".format(test_file))
... else:
...     print("file {} does not exists".format(test_file))
...
file test.txt exists
-bash-4.2$ ls
python_samples  test.txt

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pathlib
>>> test_file = 'test1.txt'
>>> file = pathlib.Path(test_file)
>>> if file.exists():
...     print("file {} exists".format(test_file))
... else:
...     print("file {} does not exists".format(test_file))
...
file test1.txt does not exists
>>>

In a nutshell

  • Use path.exists to verify if the given file exists.
  • Use path.isfile to check whether a path is file.
  • The version Python 3.4 and above provides a pathlib Module to verify the existence of file.

Along with the above-mentioned methods, there is another straight forward pythonic way for checking the existence of the file. Use the open() method to open the file.

-bash-4.2$ ls
python_samples  test.txt

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> try:
...     open('text.txt')
... except:
...     print("file does not exists")
...
file does not exists
-bash-4.2$ ls
python_samples  test.txt

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> file_name = 'test.txt'
>>> try:
...     with open(file_name) as f:
...             print("{} exists".format(file_name))
... except:
...     print("{} does not exists".format(file_name))
...
test.txt exists
>>>

Usage of with in above example, ensures the file is closed after the file operation.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.