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_typefunction 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.
- Open the
file_type.pyfile in your text editor. - Locate the
count_file_typefunction definition. - 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
countvariable for each file that matches the specified file type. - Return the final
countvalue.
- Use the
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.
Open a terminal or command prompt and navigate to the directory containing the
file_type.pyfile.Run the script using the following command:
python3 file_type.pyThe script will prompt you to enter the file type:
Please enter the file type:Type the file type (e.g., "txt") and press Enter.
$ python3 file_type.py Please enter the file type: txt 3The script will then print the number of files with the specified file type in the
/home/labex/projectdirectory.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



