Skip to main content

Folder Size

note

Difficulty: ⭐⭐⭐⭐

Default windows does not have a feature to show the sizes of folders. To implement this ourselves we must recursively query the sizes of the files inside the directory to the get the size of the folder.

Running the program​

Imagine you have the file structure:

📂mystuff
┣ 📜size.py
┣ 📜data.csv
┗ 📂levels
┣ 📜first.txt
┗ 📜second.txt

This is what the execution of the program would be if the working directory was mystuff.

$ python size.py
📂  levels    (437000 bytes)
📜 data.csv (256 bytes)
📜 size.py (15 bytes)

Command-line argument​

An optional command-line argument can be added which will instead choose which folder on your computer to read from:

$ python size.py levels
📜  first.txt   (400000 bytes)
📜 second.txt (37000 bytes)

If the command-line argument directory does not exist instead print:

Directory does not exist

If the command-line argument directory is not a directory, but instead a file print:

Selected file is not a directory

If the folder is empty, print:

Folder is empty

OS functions​

These are some functions that will be useful in this challenge:

  • os.walk()
  • os.stat()
  • os.path.abspath()

Obtaining the sizes of directories requires you to recursively sum the size of the files and directories inside said directory.

Any other details are free to be interpreted. This program aims to challenge you to use a library not covered in the tutorials. This challenge is hard to complete without recursion.