

Listing 3: Listing files using os and fnmatch module import os, fnmatch Listing 3 contains the Python script, and Example 3 the corresponding output. In a for loop we iterate over the list of entries stored in the variable listOfFiles.įinally, with the help of fnmatch we filter for the entries we are looking for, and print the matching entries to stdout. Next, we define the directory we would like to list the files using os.listdir(), as well as the pattern for which files to filter. This variant works with Python 2 and 3, too.Īs the first step, we import the two modules os, and fnmatch. Instead, let us combine the methods from the two modules os, and fnmatch. Combining os and fnmatchĪs you have seen before the solution using subprocesses is elegant but requires lots of code. This solution works quite well with both Python 2 and 3, but can we improve it somehow? Let us have a look at the other variants, then. # output the files line by line for line in endOfPipe:Įxample 2: Running the program $ python find-files3.py Listing 2: Defining two processes connected with a pipe Finally, the variable endOfPipe reads the output of grep from grep.stdout that is printed to stdout element-wise in the for-loop below.

To read the output of the ls command from the pipe, the stdin channel of grep is defined as ls.stdout. The second variable grep is defined as a process, too, but executes the command grep -v /$, instead. That's why the stdout channel is defined as subprocess.PIPE. The first variable ls is defined as a process executing ls -p. Calling the method subprocess.Popen() opens a corresponding process, and defines the two parameters named stdin and stdout. The subprocess module allows to build real pipes, and to connect the input and output streams as you do on a command line. Actually, /$ is a Regular Expression that matches all the strings that contain the character / as the very last character before the end of the string, which is represented by $. The parameters -v /$ exclude all the names of entries that end with the delimiter /. The output of this call is piped to the grep command that filters the data as we need it. lists directory files for the current directory, and adds the delimiter / at the end of the name of each subdirectory, which we'll need in the next step. The system command we call in this case is the following one:Įxample 1: Listing the files in the current directory $ ls -p. Note: While this is a valid way to list files in a directory, it is not recommended as it introduces the opportunity for command injection attacks.Īs already described in the article Parallel Processing in Python, the subprocess module allows you to execute a system command, and collect its result. Listing 1: Traversing the current directory using os.walk() import os This works with both Python 2 and 3 interpreters.
PYTHON3.6 LIST DIRECTORY CONTENTS HOW TO
Listing 1 shows how to write this with only three lines of code.

It contains the name of the root directory, a list of the names of the subdirectories, and a list of the filenames in the current directory. One of them is walk(), which generates the filenames in a directory tree by walking the tree either top-down or bottom-up (with top-down being the default setting). The os module contains a long list of methods that deal with the filesystem, and the operating system. The following solutions demonstrate how to use these methods effectively. To simply list files in a directory the modules os, subprocess, fnmatch, and pathlib come into play. This also includes file system functions. I prefer to work with Python because it is a very flexible programming language, and allows me to interact with the operating system easily.
