Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to check whether a file or directory exists

To check if a file or directory exists in Python, you can use the os.path.exists() function from the os module. This function returns True if the given path exists, and False otherwise.

Here's an example of how to check if a file or directory exists using the os.path.exists() function:

import os

file_path = "file.txt"
directory_path = "example_directory"

# Check if the file exists
if os.path.exists(file_path):
    print(f"The file '{file_path}' exists.")
else:
    print(f"The file '{file_path}' does not exist.")

# Check if the directory exists
if os.path.exists(directory_path):
    print(f"The directory '{directory_path}' exists.")
else:
    print(f"The directory '{directory_path}' does not exist.")

In this example, we first import the os module. We then use the os.path.exists() function to check if the file and directory paths exist.

If you want to check specifically for a file or a directory, you can use os.path.isfile() or os.path.isdir():

import os

file_path = "file.txt"
directory_path = "example_directory"

# Check if the path is a file
if os.path.isfile(file_path):
    print(f"The path '{file_path}' is a file.")
else:
    print(f"The path '{file_path}' is not a file.")

# Check if the path is a directory
if os.path.isdir(directory_path):
    print(f"The path '{directory_path}' is a directory.")
else:
    print(f"The path '{directory_path}' is not a directory.")

In this example, we use os.path.isfile() to check if the given path is a file, and os.path.isdir() to check if the given path is a directory.

  1. Python Check if File Exists:

    import os
    
    file_path = "example.txt"
    
    if os.path.exists(file_path):
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  2. Check if Directory Exists in Python:

    import os
    
    directory_path = "example_directory"
    
    if os.path.exists(directory_path) and os.path.isdir(directory_path):
        print(f"The directory '{directory_path}' exists.")
    else:
        print(f"The directory '{directory_path}' does not exist.")
    
  3. Python os.path.exists() Example:

    import os
    
    file_path = "example.txt"
    
    if os.path.exists(file_path):
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  4. How to Verify File Existence in Python:

    import os
    
    file_path = "example.txt"
    
    if os.path.isfile(file_path):
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  5. Check if File is Present Using os Module in Python:

    import os
    
    file_path = "example.txt"
    
    if os.path.exists(file_path):
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  6. Python pathlib Check if File Exists:

    from pathlib import Path
    
    file_path = Path("example.txt")
    
    if file_path.exists():
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  7. Determine if Directory Exists in Python:

    import os
    
    directory_path = "example_directory"
    
    if os.path.exists(directory_path) and os.path.isdir(directory_path):
        print(f"The directory '{directory_path}' exists.")
    else:
        print(f"The directory '{directory_path}' does not exist.")
    
  8. Check if a File or Folder Exists in Python:

    import os
    
    path = "example_path"
    
    if os.path.exists(path):
        if os.path.isfile(path):
            print(f"The file '{path}' exists.")
        elif os.path.isdir(path):
            print(f"The directory '{path}' exists.")
    else:
        print(f"The path '{path}' does not exist.")
    
  9. Python os.path.isfile() and os.path.isdir() Usage:

    import os
    
    path = "example_path"
    
    if os.path.exists(path):
        if os.path.isfile(path):
            print(f"The file '{path}' exists.")
        elif os.path.isdir(path):
            print(f"The directory '{path}' exists.")
    else:
        print(f"The path '{path}' does not exist.")
    
  10. How to Handle File Existence Check in Python:

    import os
    
    file_path = "example.txt"
    
    try:
        with open(file_path, 'r'):
            print(f"The file '{file_path}' exists and is readable.")
    except FileNotFoundError:
        print(f"The file '{file_path}' does not exist.")
    
  11. Verify File Presence with os.path in Python:

    import os
    
    file_path = "example.txt"
    
    if os.path.exists(file_path):
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  12. Python try-except Block for File Existence Check:

    import os
    
    file_path = "example.txt"
    
    try:
        with open(file_path, 'r'):
            print(f"The file '{file_path}' exists and is readable.")
    except FileNotFoundError:
        print(f"The file '{file_path}' does not exist.")
    
  13. Check if Path Exists Using os.path in Python:

    import os
    
    path = "example_path"
    
    if os.path.exists(path):
        print(f"The path '{path}' exists.")
    else:
        print(f"The path '{path}' does not exist.")
    
  14. Python os.path.exists() vs os.path.isfile():

    import os
    
    file_path = "example.txt"
    
    if os.path.exists(file_path):
        print(f"The path '{file_path}' exists.")
        if os.path.isfile(file_path):
            print(f"It is a file.")
        else:
            print(f"It is a directory.")
    else:
        print(f"The path '{file_path}' does not exist.")
    
  15. Conditional Check for Directory Existence in Python:

    import os
    
    directory_path = "example_directory"
    
    if os.path.exists(directory_path) and os.path.isdir(directory_path):
        print(f"The directory '{directory_path}' exists.")
    else:
        print(f"The directory '{directory_path}' does not exist.")
    
  16. Using pathlib.Path to Check File Existence in Python:

    from pathlib import Path
    
    file_path = Path("example.txt")
    
    if file_path.exists():
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  17. Error Handling When Checking File or Directory Existence in Python:

    import os
    
    file_path = "example.txt"
    
    try:
        with open(file_path, 'r'):
            print(f"The file '{file_path}' exists and is readable.")
    except FileNotFoundError:
        print(f"The file '{file_path}' does not exist.")
    
  18. Python os.path and os.path.join for Path Existence Check:

    import os
    
    directory = "example_directory"
    filename = "example.txt"
    file_path = os.path.join(directory, filename)
    
    if os.path.exists(file_path):
        print(f"The file '{file_path}' exists.")
    else:
        print(f"The file '{file_path}' does not exist.")
    
  19. Check if File Exists and Is Readable in Python:

    import os
    
    file_path = "example.txt"
    
    try:
        with open(file_path, 'r'):
            print(f"The file '{file_path}' exists and is readable.")
    except FileNotFoundError:
        print(f"The file '{file_path}' does not exist.")
    except IOError:
        print(f"The file '{file_path}' exists but is not readable.")