Working with the filesystem in Python
When working with files it is important to be able to move through the filesystem, determine the type of file, and open a file in the different modes offered by the operating system.
Throughout this section, we explain the main modules you can find in Python for working with the filesystem, accessing files and directories, reading and creating files, and carrying out operations with the context manager.
Working with files and directories
As we have seen in the previous section, it can be interesting to find new folders 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)
We can check whether a certain string is a file or directory. For this task we can use the os.path.isfile()
method, which returns True...