

Also, note that this function returns the file paths in arbitrary order. This returns the full (relative) paths, so no need for os.path.join(). This example with glob.glob() shows how to match all JPEGs in a specific folder: This might look like regular expressions, but it is more limited. Additionally, you can have sets of characters defined within, like with which matches a single number. Two common patterns are * which matches with 0 or more arbitrary characters and ? which matches with a single arbitrary character. It uses the Unix style pathname pattern expansion, commonly referred to as glob patterns which you would use when working with Linux/macOS. You can read in the article by Ban Hoyt about his story how this was added to the Python standard library.Īnother module that you can use to traverse the file system is the glob module. As of Python 3.5, the os.walk() function uses os.scandir() in the background to increase the speed quite significantly. It is also possible to walk the file tree bottom up by adding the argument topdown=False to the os.walk() function. Keep in mind that as with os.listdir(), you iterate over each file name, which means that you have to join the directory path dirpath with the file name or directory name. join ( dirpath, f )) for d in dirnames : print ( 'DIRECTORY :', os. walk ( '.' ): for f in filenames : print ( 'FILE :', os.

Here is how you can get a list of all files and directories:įor ( dirpath, dirnames, filenames ) in os. The simplest way is by using os.listdir() which lists all filenames and directories in a given folder path. In Python, you have a number of ways to traverse the file system. In the last section, you will see a practical way to analyze folder structures with folderstats and Pandas with some use cases and visualizations along the way. In the next section, you will see how to extract statistics from files and directories. In this article, you will learn the various ways to traverse and explore the file system with Python.

How can you make sense of this mess? Python offers various tools in the Python standard library to deal with your file system and the folderstats module can be of additional help to gain insights into your file system. Say you have an external hard drive with layers upon layers of cryptically named folders and intricate mazes of directories (like here, or here). Explore and Visualize the Folder Structure as a Graph.Exploring File Distributions and Zipf’s Law.
