Data aggregation is the most essential operation when working with trees. Examples of data aggregation are:
- Calculating the total number of files in the directory or the overall size of all files
- Getting a list of all files
- Finding all files by template
The point in aggregating operations is accumulating the result. Traversing the tree in depth using a recursive process, which we discussed in detail in the previous lesson, is well-suited for this. Using it, we can go through all the tree nodes and collect the result, starting from the lowest level.
Let's look at aggregation using a recursive process by counting the total number of nodes in a tree as an example. Essentially, we want to find out how many files and directories are in our file tree:
from hexlet import fs tree = fs.mkdir('/', [ fs.mkdir('etc', [ fs.mkfile('bashrc'), fs.mkfile('consul.cfg'),