Project: File & Directory Utility Tool0%

Project: File & Directory Utility Tool

Beginner12 min readUpdated: Jul 11, 2026
Study Materials

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:

  1. 1
    Storage Audit Mode (--audit):
  • Recursively scans a target directory using os.walk.
  • Aggregates storage consumption by file extension using collections.defaultdict and collections.Counter.
  • Formats raw byte counts into human-readable units (KB, MB, GB) using math.log and math.pow.
  • Identifies the top 5 largest files and timestamps of recent modifications using datetime.
  1. 1
    Auto-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.rename and os.makedirs.

2. Complete Utility Code

Visual Architecture & Process Flow

How data and code flow step-by-step

Flowchart
Step 1
def format_bytes
size_bytes: int
Step 2
str:

3. Sample Execution Simulation

Running the Audit Command:

Bash / Terminal
python file_tool.py --audit ./sample_data
Output
Analyzing directory: /workspace/sample_data
=================================================================
Total Files Scanned : 34
Total Space Consumed: 14.82 MB
-----------------------------------------------------------------
EXTENSION | COUNT | TOTAL SIZE
-----------------------------------------------------------------
.png | 12 | 8.45 MB
.pdf | 8 | 4.12 MB
.py | 10 | 185.40 KB
.csv | 4 | 2.07 MB
 
--- Top 5 Largest Files ---
1. annual_report.pdf | 3.85 MB | Modified: 2026-09-10 11:24
2. screenshot_hd.png | 2.45 MB | Modified: 2026-09-11 14:15
3. sales_q3.csv | 1.80 MB | Modified: 2026-09-12 09:30
4. banner.png | 1.20 MB | Modified: 2026-09-08 17:40
5. client_mockup.png | 950.00 KB | Modified: 2026-09-09 16:05
=================================================================

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.


Next Lesson

Introduction to Databases

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext