Python File Type Counter

PythonBeginner
Practice Now

Introduction

In this project, you will learn how to write a Python script that counts the number of files with a specific file type in a given directory. This is a common task when working with files and directories in Python, and it can be useful in a variety of applications, such as data analysis, file management, and system administration.

👀 Preview

$ python3 file_type.py
Please enter the file type: txt
3

🎯 Tasks

In this project, you will learn:

  • How to implement the count_file_type function to count the number of files with a specific file type
  • How to handle user input and display the file type count
  • How to enhance the script to handle multiple file types and directory access errors

🏆 Achievements

After completing this project, you will be able to:

  • Write a Python script that counts the number of files with a specific file type in a directory
  • Handle user input and display the file type count
  • Enhance the script to handle edge cases and provide more functionality

Implement the count_file_type Function

In this step, you will implement the count_file_type function in the file_type.py file. This function will count the number of files with a specific file type in the given directory.

  1. Open the file_type.py file in your text editor.
  2. Locate the count_file_type function definition.
  3. Implement the function logic to count the number of files with the specified file type in the given directory.
    • Use the os.listdir() function to get a list of all files in the directory.
    • Iterate through the list of files and check if each file ends with the specified file type using the endswith() method.
    • Increment the count variable for each file that matches the specified file type.
    • Return the final count value.

Here's the completed count_file_type function:

def count_file_type(file_path, file_type):
    """
    Count the number of files with a specific file type in the given directory.

    Args:
    - file_path: A string that specifies the directory path to count the files in.
    - file_type: A string that specifies the file type (file extension) to count.

    Returns:
    - count: An integer representing the number of files with the specified file type in the directory.
    """
    count = 0
    for file in os.listdir(file_path):
        if file.endswith("." + file_type):
            count += 1
    return count

Run the Script and Get User Input

In this step, you will run the file_type.py script and prompt the user to enter the file type they want to count.

  1. Open a terminal or command prompt and navigate to the directory containing the file_type.py file.

  2. Run the script using the following command:

    python3 file_type.py
    
  3. The script will prompt you to enter the file type:

    Please enter the file type:
    
  4. Type the file type (e.g., "txt") and press Enter.

    $ python3 file_type.py
    Please enter the file type: txt
    3
    
  5. The script will then print the number of files with the specified file type in the /home/labex/project directory.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

✨ Check Solution and Practice✨ Check Solution and Practice