The library we use to build trees is designed only for immutable file structures. It means we cannot change it once we create it. But it is possible to make a new one based on the old one and modify some parts.
We deliberately chose to use an immutable structure for this course. This structure is easier to debug, and you're less likely to make mistakes. And it allows you to immerse yourself as much as possible in higher-order functions.
Basic operations with nodes
The python-immutable-fs-trees package allows you to create trees and extract data from previously created files and directories. It means you don't have to crawl through the internal structure of the tree itself:
from hexlet import fs
tree = fs.mkdir('/', [fs.mkfile('hexlet.log')], {'hidden': True})
fs.get_name(tree)
# '/'
fs.get_meta(tree).get('hidden')
# True
[file] = fs.get_children(tree)
fs.get_name(file)
# 'hexlet.log'
fs.get_meta(file).get('unknown')
# Don't do this
# Files have no children
fs.get_children(file)
Additionally, the package has two functions for checking types. Using them, you can selectively work with files and directories:
from hexlet import fs
tree = fs.mkdir('/', [fs.mkfile('hexlet.log')], {'hidden': True})
fs.is_directory(tree)
# True
fs.is_file(tree)
# False
[file] = fs.get_children(tree)
fs.is_file(file)
# True
fs.is_directory(file)
# False
The operations we have looked at are enough to perform any transformations on files and directories. Let us start with the simplest ones, which don't require recursive traversal.