Write a python program to check whether the user given input is equal to the current working directory or not

1. Introduction

This blog post will introduce a Python program that checks if the user's input matches the current working directory. This is a practical example of interacting with the file system using Python, and it can be useful in various applications where path validation is required.

2. Program Steps

1. Import the os module to work with the operating system.

2. Get the current working directory using os.getcwd().

3. Prompt the user to input a directory path.

4. Compare the user's input with the current working directory.

5. Return or print whether they match.

3. Code Program


import os

def check_current_directory():
    # Step 2: Get the current working directory
    current_directory = os.getcwd()

    # Step 3: Prompt the user for input
    user_input = input("Enter a directory path to check: ")

    # Step 4: Compare the user's input with the current directory
    if user_input == current_directory:
        print("Your input matches the current working directory.")
    else:
        print("Your input does not match the current working directory.")

# Call the function
check_current_directory()

Output:

The output will depend on the user's input and the actual current working directory. It will either confirm a match or state that there is no match.

Explanation:

1. The program begins by importing the os module, which provides a way of using operating system dependent functionality.

2. It uses os.getcwd() to obtain the current working directory, which is the directory from which the script is being run.

3. The user is prompted to enter a directory path, which is stored in user_input.

4. The program then compares user_input with current_directory using a simple if statement.

5. Depending on the comparison, it prints whether the user's input matches the current working directory or not.

This program effectively demonstrates basic file system interaction in Python, particularly checking the current working directory against user input.


Comments