os.listdir() method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned.
Syntax: os.listdir(path)
Parameters:
path (optional) : path of the directoryReturn Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.
Code #1: use of os.listdir() method
# Python program to explain os.listdir() method # importing os module import os # Get the list of all files and directories # in the root directory path = "/"dir_list = os.listdir(path) print("Files and directories in '", path, "' :") # print the list print(dir_list) |
Files and directories in ' / ' : ['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr', 'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']
Code #2: use of os.listdir() method
# Python program to explain os.listdir() method # importing os module import os # Get the path of current working directory path = os.getcwd() # Get the list of all files and directories # in current working directory dir_list = os.listdir(path) print("Files and directories in '", path, "' :") # print the list print(dir_list) |
Files and directories in ' /home/ihritik ' : ['.rstudio-desktop', '.gnome', '.ipython', '.cache', '.config', '.ssh', 'Public', 'Desktop', '.pki', 'R', '.bash_history', '.Rhistory', '.oracle_jre_usage', 'Music', '.ICEauthority', 'Documents', 'examples.desktop', '.swipl-dir-history', '.local', '.gnupg', '.profile', 'Pictures', '.keras', '.viminfo', '.thunderbird', 'Templates', '.bashrc', '.bash_logout', '.sudo_as_admin_successful', 'Videos', 'images', 'tf_wx_model', 'Downloads', '.mozilla', 'geeksforgeeks']
Code #3: omitting path parameter
# Python program to explain os.listdir() method # importing os module import os # If we do not specify any path # os.listdir() method will return # the list of all files and directories # in current working directory dir_list = os.listdir() print("Files and directories in current working directory :") # print the list print(dir_list) |
Files and directories in current working directory : ['.rstudio-desktop', '.gnome', '.ipython', '.cache', '.config', '.ssh', 'Public', 'Desktop', '.pki', 'R', '.bash_history', '.Rhistory', '.oracle_jre_usage', 'Music', '.ICEauthority', 'Documents', 'examples.desktop', '.swipl-dir-history', '.local', '.gnupg', '.profile', 'Pictures', '.keras', '.viminfo', '.thunderbird', 'Templates', '.bashrc', '.bash_logout', '.sudo_as_admin_successful', 'Videos', 'images', 'tf_wx_model', 'Downloads', '.mozilla', 'geeksforgeeks']
As we can see the output of Code #2 and Code #3 are same. So if we omit the path parameter os.listdir() method will return the list of all files and directories in the current working directory.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Difference between Method Overloading and Method Overriding in Python
- class method vs static method in Python
- Get() method for dictionaries in Python
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Line detection in python with OpenCV | Houghline method
- Python | os._exit() method
- Python | os.WEXITSTATUS() method
- Python | os.abort() method
- Python | os.renames() method
- Python | os.lseek() method
- Python calendar module | formatmonth() method
- Python | PyTorch sin() method
- Python | Sympy Line.is_parallel() method
- Python PIL | GaussianBlur() method
- Python | range() method
- Python | Numpy np.hermefit() method
- Python | Numpy np.hermevander() method
- Python String Title method
- Python groupby method to remove all consecutive duplicates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

