Project: File & Directory Utility Tool
Project: File & Directory Utility Tool
In this capstone project, we will unite the key Python Standard Libraries mastered across this chapter—os, sys, datetime, collections, and math—to build a versatile, production-ready Command-Line File & Directory Management Utility.
1. Project Overview & Capabilities
System administrators and developers often need tools to audit storage usage and organize cluttered directories. Our utility provides two operational modes:
- 1Storage Audit Mode (
--audit):
- Recursively scans a target directory using
os.walk. - Aggregates storage consumption by file extension using
collections.defaultdictandcollections.Counter. - Formats raw byte counts into human-readable units (KB, MB, GB) using
math.logandmath.pow. - Identifies the top 5 largest files and timestamps of recent modifications using
datetime.
- 1Auto-Organizer Mode (
--organize):
- Scans a target folder and automatically classifies loose files into categorical subdirectories (
Images,Documents,Code,Archives,Audio_Video,Others) based on extension. - Moves files safely using
os.renameandos.makedirs.
2. Complete Utility Code
Visual Architecture & Process Flow
How data and code flow step-by-step
3. Sample Execution Simulation
Running the Audit Command:
Multiple Choice Questions
1. In this project, which Python module is utilized to recursively traverse subfolders and files?
A. sys B. os (via os.walk) C. math D. collections Answer: B Explanation: os.walk(target_path) generates directory trees recursively, returning tuples of (root, directories, files).
2. How does format_bytes() calculate the appropriate magnitude unit (B, KB, MB, GB)?
A. By dividing by 1000 repeatedly in a for loop B. By using math.log(size_bytes, 1024) to determine unit index mathematically C. By hardcoding file sizes in an array D. By calling an operating system shell script Answer: B Explanation: Calculating the logarithm in base 1024 determines the exact magnitude power index i in $O(1)$ time.
3. Which data structure from the collections module was used to tally how many files exist per extension?
A. namedtuple B. deque C. Counter D. OrderedDict Answer: C Explanation: collections.Counter automatically tracks frequencies and provides convenient methods like .most_common().
4. Why does organize_directory() call os.makedirs(category_dir, exist_ok=True)?
A. To format the disk drive B. To create the category folder if it doesn't already exist without raising a FileExistsError C. To delete temporary files D. To compress the folder into a zip file Answer: B Explanation: Setting exist_ok=True ensures the directory is created if missing, and suppresses exceptions if the directory already exists.
5. How are the modification timestamps extracted from files converted into readable dates?
A. datetime.fromtimestamp(stat_info.st_mtime) B. os.path.readable_date() C. time.convert() D. sys.get_time() Answer: A Explanation: os.stat().st_mtime provides epoch seconds, which datetime.fromtimestamp() converts into a Python datetime object for formatting.
Introduction to Databases
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| OS and Sys Modules | Introduction to Databases |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.