os.listdir() Method - Python | Explained with Examples


os.listdir() is an integral feature of Python's os package. The os module contains interfaces for the operating system.

Syntax -
os.listdir()
os.listdir(path)

path: This is an optional input parameter that we can pass to the os.listdir() method.

os.listdir() Method - Python

Python's os.listdir() method retrieves a list of all files and directories in a given directory (if the path to the given directory is specified).

os.listdir(path)

list of the files and directories in the current working directory is returned if no directory is specified.

os.listdir()

The list is arranged arbitrarily or randomly. This method's return type is a list.

Even if the unique entries '.' and '..' are present in the directory, they are omitted.

Let's understand os.listdir() better with examples. We will consider a directory - 'Python' inside our D Drive for our examples.

The figure below depicts our directory structure and the files included within it.

os.listdir() Method - Directory Structures

Our Python file - OurCode.py, is inside the folder 'Directory 2.'

Note: We must import the os module before using the os.getcwd() method.

Example 1

Let's print the files and directories inside our Python folder. We will also print the return type of the os.listdir() method.

Here is the Python Code -

import os

path = "D:\Python"
list_a = os.listdir(path)

print(list_a) 
print(type(list_a)) #This will print the return type of list_a

The Output will be - 

['Directory 1', 'Directory 2', 'File 1.jpg', 'File 2.pn', 'File 3.jpg', 'File 4.pdf', 'File 5.png']
<class 'list' >

Example 2

In this example, we will pass the path as '/', the current working directory.

We will also use the os.listdir() method without passing any input parameter.

Here is the Python Code -

import os

path_1 = "/"
list_a = os.listdir(path_1) # Passing an Empty path will retrieve the directory - D Drive

path_2 = os.getcwd() # getcwd() will retrieve the Current Working Directory i.e., 'Directory 2'
list_b = os.listdir(path_2)

list_c = os.listdir() # Here we do not pass any path

print(list_a)
print(list_b)
print(list_c)

Output #1 - When our working directory is 'Python'

['$RECYCLE.BIN', 'Back Ups', 'Python', 'System Volume Information']
['Directory 1', 'Directory 2', 'File 1.jpg', 'File 2.pn', 'File 3.jpg', 'File 4.pdf', 'File 5.png']
['Directory 1', 'Directory 2', 'File 1.jpg', 'File 2.pn', 'File 3.jpg', 'File 4.pdf', 'File 5.png']

Refer to the below Image for a clear understanding -

os.getcwd() inside folder python

Output #2 - When our working directory is 'Directory 2', which is inside the folder 'Python'

['$RECYCLE.BIN', 'Back Ups', 'Python', 'System Volume Information']
['File 10.pn', 'File 11.pdf', 'File 9.png', 'OurCode.py']
['File 10.pn', 'File 11.pdf', 'File 9.png', 'OurCode.py']

Refer to the below Image for a clear understanding -

os.getcwd() inside folder directory 2

As we can see from the above outputs, passing '/' as a path to the os.getcwd() method will fetch the files and directories from our root drive.

Passing the current working directory or passing no input parameters to the os.getcwd() method would fetch the files and directories inside our current working directory.

Example 3

Since the os.getcwd() method returns a list, we can iterate through the list elements using for loop and perform specific actions to each item.

Here is the Python Code -

import os

path_1 = "D:/Python/Directory 1"
list_a = os.listdir(path_1) 

path_2 = "."
list_b = os.listdir(path_2) 

path_3 = ".."
list_c = os.listdir(path_3) 

for item in list_a:
    print(item)

print('-'*10)

for item in list_b:
    print(item)

print('-'*10)

for item in list_c:
    print(item)

The Output will be -

File 6.pn
File 7.png
File 8.pdf
----------
Directory 1
Directory 2
File 1.jpg
File 2.pn
File 3.jpg
File 4.pdf
File 5.png
----------
$RECYCLE.BIN
Back Ups
Python
System Volume Information

os.getcwd() list - iterate through the list elements using for loop

Conclusion

Using the os.getcwd() method in Python returns us arbitrarily a list of files and directories names. We can use certain conditions and loops to iterate through every element and perform specific actions. The input parameter path is optional for Python's os.getcwd() method.

I hope you found this article helpful.

os.listdir() Method – Python Explained with Examples - FI

You may also like to Explore -

os.walk() Method – Python – Explained with Examples

random choice() method – Python – with Examples

mode() method – Python statistics module

Cheers!

Happy Coding.

About the Author

This article was authored by Rawnak.