Kodeclik Blog
Python os.listdir()
The Python os.listdir() method within the os module takes a directory as argument and returns a list of all files and directories within the given directory.
Let us explore this method by creating a repl and opening a Python file.
Now let us create a Python program in it:
import os
directory = os.getcwd()
print(directory)
files = os.listdir(directory)
print(files)
To support your understanding this is how our setup looks:
In the program above, we first import the “os” module in Python. Then we obtain the current working directory (cwd) using the getcwd() command and save it in the variable called “directory”. Note that the output of this command will be different in different environments. Then we call our os.listdir() method with the current directory (that was just determined). The output of the listdir() method is a list which can be manipulated using any Python list manipulation function. The output is printed using the “print(files)” command.
The output in our case is:
/home/runner/WideeyedGiantGlitches
['.cache', '.config', '.upm', 'poetry.lock', '.breakpoints',
'replit.nix', 'venv', '.replit', 'pyproject.toml', 'main.py']
There are several items of interest here. First note that the line printing the current working directory (cwd) will be different in your case so do not fret if the output is not exactly the same. The second line prints the files. As you can see the files such as “main.py” (where the above program is stored), “poetry.lock” and “pyproject.toml” are what you see in the screenshot above. Beyond this you can see a lot of files beginning with a “dot”, and this is why we do not see them in the screenshot above. Nevertheless these files are present in the project. You can click on “Show hidden files” as shown below:
This will reveal the following:
Now you can see the files that were missing. Note that some of them are actually directories (like .config and .upm).
Using os.listdir() to pretty print all files in a directory
Because the output of os.listdir() is a list you can pretty print this list as you see fit. For instance consider the program:
import os
directory = os.getcwd()
print(directory)
files = os.listdir(directory)
for f in files:
print("I found a file called " + f)
The output is:
/home/runner/WideeyedGiantGlitches
I found a file called .cache
I found a file called .config
I found a file called .upm
I found a file called poetry.lock
I found a file called .breakpoints
I found a file called replit.nix
I found a file called venv
I found a file called .replit
I found a file called pyproject.toml
I found a file called main.py
Using os.listdir() to print all hidden files
You can take the output of os.listdir() and print all hidden files and directories (i.e., files beginning with a “dot”):
import os
directory = os.getcwd()
print(directory)
files = os.listdir(directory)
for f in files:
if (f.startswith(".")):
print("I found a hidden file/dir called " + f)
The output is:
/home/runner/WideeyedGiantGlitches
I found a hidden file/dir called .cache
I found a hidden file/dir called .config
I found a hidden file/dir called .upm
I found a hidden file/dir called .breakpoints
I found a hidden file/dir called .replit
as expected.
In this blogpost, we have seen how Python’s os.listdir() function can help explore directories from within your Python program. If you liked this, checkout our blogpost on the Python os.mkdir() function which as you can surmise helps create directories! Also checkout the Python os.join.path() function which can help create long directory paths. Also learn how to delete files in Python!
For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.