Python Directory Listing

Python Directory Listing

In this article, we’ll look at how we can perform Python directory listing. This will allow us to list all the files and directories in the current working location.

Often. we may want to quickly look at the filenames and get information using Python.

Let’s look at how we can do it quickly and easily!


1. Python Directory Listing Using os.listdir()

This is a short and sweet method to perform Python directory listing, from your current directory!

It’s really just one line. Don’t believe me? Here’s an example. This applies to any operating system, whether it be Windows / Linux / MacOS.

import os

print(os.listdir())

Example Output

>>> import os
>>> os.listdir()
['.bashrc', '.git', '.nvimrc', '.vimrc', '.xinitrc', '.zshrc', 'Autumn.jpg', 'README.md', 'config']

This will return a list of all files and nested folders, from your current directory.

If you want to specify an exact path, you can simply pass it as an argument to os.listdir(path)!

>>> os.listdir(r'/home/vijay/manjaro-dotfiles')
['.bashrc', '.git', '.nvimrc', '.vimrc', '.xinitrc', '.zshrc', 'Autumn.jpg', 'README.md', 'config']

Use raw strings (strings prefixed with r) when you’re dealing with paths, since you won’t need to escape any backslashes (for Windows paths).

2. Use os.path.join() with os.listdir()

If you want to print the absolute path of all the files from your current directory, simply add an os.path.join() to the os.listdir() function!

We’ll make a function for this, which simply gets the full path, and returns a list of all such names.

import os

def list_full_paths(directory):
    return [os.path.join(directory, file) for file in os.listdir(directory)]

print(list_full_paths(r'/home/accornition/manjaro-dotfiles'))

Output

['/home/vijay/manjaro-dotfiles/.bashrc', '/home/vijay/manjaro-dotfiles/.git', '/home/vijay/manjaro-dotfiles/.nvimrc' , '/home/vijay/manjaro-dotfiles/.vimrc', '/home/vijay/manjaro-dotfiles/.xinitrc', '/home/vijay/manjaro-dotfiles/.zsh    rc', '/home/vijay/manjaro-dotfiles/Autumn.jpg', '/home/vijay/manjaro-dotfiles/README.md', '/home/vijay/manjaro-dotfiles/config'] 

Indeed, this gives us the absolute path, from the root directory!

3.Python Directory Listing Using os.walk()

We can also use the os.walk() function to walk through the directory tree.

We can then print the directories and files individually.

for top, dirs, files in os.walk(os.getcwd()):
    print("Printing directories...")
    for dir in dirs:
        print(os.path.join(top, dir))
    print("Printing files....")
    for file in files:
        print(os.path.join(top, file))

Output

Printing directories...
/home/vijay/manjaro-dotfiles/config/cmus                                                                            /home/vijay/manjaro-dotfiles/config/compton                                                                         /home/vijay/manjaro-dotfiles/config/termite                                                                           Printing files....
Printing directories...
Printing files....                                                                                                   /home/vijay/manjaro-dotfiles/config/cmus/my.theme                                                                    Printing directories...
Printing files....
/home/vijay/manjaro-dotfiles/config/compton/compton.conf                                                             Printing directories...
Printing files....
/home/vijay/manjaro-dotfiles/config/termite/config 

You can use any of the above three methods, depending on your use-case scenario.

The first method is the easiest and the recommended one, but if you want the full path, and want to travel recursively, use os.walk().


Conclusion

In this article, we learned how we could list files and directories in Python, using different methods.

References