Working with the filesystem in Python
When working with files, it is important to be able to move through the filesystem and determine the type of file using the os
module.
Also, you may want to traverse the filesystem or determine where files are to manipulate them. Throughout this section, we explain how we can work with the filesystem, accessing files, and directories, and how we can work with ZIP files.
Working with files and directories
As we have seen in the previous section, it can be interesting to find new folders by iterating recursively through the main directory. In this example, we see how we can recursively search inside a directory and get the names of all files inside that directory:
>>> import os
>>> file in os.walk("/directory"):
>>> print(file)
Also, we can execute other tasks like checking whether a certain string is a file or directory. For this task, we can use the os.path.isfile()
method, which returns...