Change language

Check if a directory contains a file using python

| |

You can find out if a directory is empty or not in Python with

Returns: A list of files present in the directory, empty list if the directory is empty

Now, by calling listdir () , we can get a list of all files present in the directory. To check the emptyness of the directory, we must check the emptyness of the returned list. We have many ways to do this, let’s test them one by one.

  1. Comparing the returned list with a hard-coded empty list
    An empty list can be written as [] . Thus, we can compare the equality of the returned list with .

    # Python program to check
    # if the directory contains the file

     

     

    import os

     
    # directory path

    directoryPath = "D: / Pycharm projects / Python.Engineering / Nikhil"

     
    # Compare returned list with empty list

    if os.listdir (directoryPath) = = []:

    print ( "No files found in the directory." )

    else :

    print ( "Some files found in the directory." )

    Exit:

     Some files found in the directory. 
  2. Comparing the length of the returned list with 0
    We can get the length of the list using Python’s len () method. If the length of the returned list is zero, then the directory is empty, otherwise it is not.

    # Python checker
    # the directory contains the file

     

     

    import os

     
    # directory path

    directoryPath = "D: / Pycharm projects / Python.Engineering / Nikhil"

     
    # Check list length

    if len (os.listdir (directoryPath)) = = 0 :

    print ( "No files found in the directory." )

    else :

      print ( "Some files found in the directory." )

    Exit:

     Some files found in the directory. 
  3. Comparing the Boolean value of a list
    In the above method, we used an explicit comparison of the length of the list. We now go in a more Pythonic way using the truth check of a value. An empty list evaluates to false in Python.

    # Python checker
    # directory is empty

     

     

    import os

     
    # directory path

    directoryPath = "D: / Pycharm projects / Python.Engineering / Nikhil"

     
    # Checking the boolean value of the list

    if not os.listdir (directoryPath):

    prin t ( "No files found in the directory." )

    else :

    print ( " Some files found in the directory. " )

    Exit :

     Some files found in the directory. 

Full source code:

# Python checker
# directory is empty

 

import os

 

  
# Function to check if the directory
# contains a file or not

def isEmpty (directoryPath):

 

# Check if the directory exists or not

if os.path.exists (directoryPath):

 

# Checking if the directory is empty or not

if len (os.listdir (directoryPath)) = = 0 :

return "No files found in the directory."

else :

return "Some files found in the directory."

else :

return   "Directory does not exist!"

 
# Driver code

  
# Valid directory

directoryPath = "D: / Pycharm projects / Python.Engineering / Nikhil"

print ( "Valid path:" , isEmpty (directoryPath))

  
# Invalid directory

directoryPath = "D: / Pycharm projects / Python.Engineering / Nikhil / GeeksforGeeks"

print ( "Invalid path:" , isEmpty (directoryPath))

Exit :

 Valid path: Some files found in the directory. Invalid path: Directory does not exist! 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method