Python File Type Counter

PythonPythonBeginner
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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/with_statement -.-> lab-302727{{"`Python File Type Counter`"}} python/conditional_statements -.-> lab-302727{{"`Python File Type Counter`"}} python/for_loops -.-> lab-302727{{"`Python File Type Counter`"}} python/tuples -.-> lab-302727{{"`Python File Type Counter`"}} python/function_definition -.-> lab-302727{{"`Python File Type Counter`"}} python/importing_modules -.-> lab-302727{{"`Python File Type Counter`"}} python/standard_libraries -.-> lab-302727{{"`Python File Type Counter`"}} python/os_system -.-> lab-302727{{"`Python File Type Counter`"}} python/build_in_functions -.-> lab-302727{{"`Python File Type Counter`"}} end

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.

Other Python Tutorials you may like