Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python Directory Listing

  • Vijaykrishna RamVijaykrishna Ram
  • April 24, 2020
  • Python Programming Examples
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

  • StackOverflow question on listing contents from a Directory

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Tkinter Text Widget with Tkinter Scrollbar
Previous Post Tkinter Text Widget with Tkinter Scrollbar
Next Post 3 Ways to Sort a Dictionary by Value in Python
3 Ways to Sort a Dictionary by Value in Python

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.