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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically