Background Info
Iterating through directories is a key part in file manipulation and automating certain actions, or finding a file type. Python provides several methods to traverse a directory, and includes:
os.listdir()os.scandir()os.walk()globWhy is this important?
Traversing directories is a useful technique when you need to automate a process.
Goal
The goal of this post is to demonstrate how to traverse a directory and merge PDF files in each directory. This is an example of how tedious work can be automated. One of the tasks I've had at work is to prepare multiple documents for payment processing, but they needed to be saved in unique folders, and the files used to prepare the final payment documents need to be merged. Having to manually do this is labor intensive, and a big waste of time. This simple code works for cases where you just need to merge several pdf files, and you are traversing folders in the same directory. If you have hundreds of folders,this code will need to be optimized (perhaps running by blobs).