25
Lesson 25
Mini-Project: File management system
Objective
By the end of this mini-project, students will have built a simple file management tool that can search, read, and manage files on their system. They will apply their knowledge of file handling, configuration, and exception handling in Python.
1. Introduction to the Mini-Project:
- Overview of the project and its objectives.
- Importance of file management systems in software applications.
- Brief discussion on the functionalities to be implemented: searching for files, reading file content, and managing files (delete, rename, etc.).
2. Project setup:
Creating a project directory:
- Set up a directory structure for the project.
- Include folders for source code, configuration files, and documentation.
By the end of this mini-project, students will have built a simple file management tool that can search, read, and manage files on their system. They will apply their knowledge of file handling, configuration, and exception handling in Python.
1. Introduction to the Mini-Project:
- Overview of the project and its objectives.
- Importance of file management systems in software applications.
- Brief discussion on the functionalities to be implemented: searching for files, reading file content, and managing files (delete, rename, etc.).
2. Project setup:
Creating a project directory:
- Set up a directory structure for the project.
- Include folders for source code, configuration files, and documentation.
file_management_system/ │ ├── src/ │ └── file_manager.py │ ├── config/ │ └── settings.json │ └── README.md
Installing necessary modules: Ensure Python is installed and introduce any necessary third-party modules (if any).
3. Functionality breakdown:
Discuss the main functionalities of the file management tool:
3.1 Searching for files:
Use the 'os' and 'fnmatch' modules to implement a function that searches for files based on a given pattern.
import os import fnmatch def search_files(directory, pattern): matches = [] for root, dirs, files in os.walk(directory): for filename in fnmatch.filter(files, pattern): matches.append(os.path.join(root, filename)) return matches
3.2 Reading file content:
Implement a function to read and display the content of a selected file using the open() function.
def read_file(filepath): with open(filepath, 'r') as file: content = file.read() return content
3.3 Managing files:
Provide options to rename or delete files. Use os.rename() and os.remove() for these functionalities.
Example: Renaming a file
def rename_file(old_name, new_name): os.rename(old_name, new_name)
Example: Deleting a file
def delete_file(filepath): os.remove(filepath)
4. User interface:
- Create a simple text-based menu interface that allows users to interact with the file management system.
- Utilize loops and conditionals to facilitate user choices.
def display_menu(): print("File Management System") print("1. Search for files") print("2. Read a file") print("3. Rename a file") print("4. Delete a file") print("5. Exit") def main(): while True: display_menu() choice = input("Choose an option: ") # Implement functionality based on user choice if choice == '5': break
5. Exception handling:
Implement try-except blocks to handle potential errors (e.g., file not found, permission denied) during file operations.
def read_file_safe(filepath): try: with open(filepath, 'r') as file: return file.read() except FileNotFoundError: print("Error: File not found.") except PermissionError: print("Error: Permission denied.")
6. Configuration management:
Create a configuration file (e.g., settings.json) to store user preferences or default settings for the file management tool.
Example: Configuration structure in json
{ "default_directory": "/path/to/default" }
7. Final touches:
Encourage students to add additional features or improvements, such as:
- Sorting search results.
- Displaying file metadata (size, last modified).
- Implementing a graphical user interface (GUI) using libraries like Tkinter or PyQt (optional).
Conclusion
In this mini-project, students built a simple file management system using Python. They gained practical experience in file handling, exception management, and user interaction. This project serves as a foundation for more complex file management applications and reinforces their understanding of file operations in Python.