Clearing up space on the hard drive quickly

Laksh
2 min readMay 6, 2019

Use Python to find the most space-consuming folders on your PC

  1. First, we will import the necessary packages. If you don’t have these packages, simply type into the command line: pip install numpy pandas.

2. Next, define a variable as the directory/folder you want to investigate. In my case, the appdata\local folder was taking up over 10GB on my hard drive!

3. Now, we will want to make a list of folders containing all the files and folders within that folder.

In cell 3, we have used list comprehension. This does the same function as a for-loop, except in one line. More info/examples here: https://www.programiz.com/python-programming/list-comprehension

4. Let’s make a function to get the size a particular folder (by scanning every single file and folder within that folder (including folders within folders within folders etc).

5. We now make a list of the sizes of all the top-level subdirectories of a directory. We will then sort this list in descending order, and get a sorted list of indices as well. We will also make a list of folder/file names.

6. Let’s create a Pandas dataframe to summarise our findings.

7. Clearly, the Spotify cache was taking a hefty amount of space. Let’s finally construct a function which does all this in one place.

This code, with the final function, can be found on my GitHub: https://github.com/aced125/Automating_menial_tasks

Thanks for reading, and hopefully you find this useful!

--

--