Python 101: Writing a cleanup script

Digital Marketing
2 min readJul 28, 2021
Python 101: Writing a cleanup script

So hi there guys! I hope you are fine. So what is in this post? Today we will be writing a cleanup script. The idea for this post came from Mike Driscol who recently wrote a very useful post about writing a cleanup script in python. So how is my post different from his post? In my post I will be using When I used for the first time I just fell in love with it.


So there are several ways for installing may be installed using setuptools or distribute or pip:


The latest release is always updated to the Python Package Index. The source code is hosted on Github.

Finding the number of files in a directory

So our first task is to find the number of files present in a directory. In this example we will not iterate over subdirectories instead we will just count the number of files present in the top level directory. This one is simple. Here is my solution:

from path import path
d = path(DIRECTORY)
#Replace DIRECTORY with your required directory
num_files = len(d.files())

print num_files

In this script we first of all imported the path module. Then we set the num_file variable to 0. This variable is going to keep count for the number of files in our directory. Then we call the path function with a directory name. Firthermore we iterate over the files present in the root of our directory and increment the num_files variable. Finally we print the value of num_files variable.