Python | os.listdir() method
The 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 a list of files and directories in the current working directory will be returned.
os.listdir() Method Syntax in Python
Syntax: os.listdir(path)
Parameters: path (optional) : path of the directory
Return Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.
Python os.listdir() Method Example
Below are some examples of Python os.listdir() method of the OS module:
List Files and Directories in Python Using os.listdir() Method
In this example, the code uses os.listdir() to obtain a list of files and directories in the root directory (“/”). It then prints the obtained list. The output includes the files and directories present in the specified root Directory.
Python3
# importing os module import os # Get the list of all files and directories path = "/" dir_list = os.listdir(path) print ( "Files and directories in '" , path, "' :" ) # print the list print (dir_list) |
Output:
Files and directories in ' / ' :
['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr',
'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']
List Files and Directories in Current Directory Using os.listdir()
In this example, the code utilizes os.listdir() method to obtain a list of files and directories in the current working directory os.getcwd() method. It then prints the obtained list, providing information about the files and directories present in the current working directory.
Python3
# importing os module import os # Get the path of current working directory path = os.getcwd() # Get the list of all files and directories dir_list = os.listdir(path) print ( "Files and directories in '" , path, "' :" ) # print the list print (dir_list) |
Output:
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']
List All Files and Directories When No Path is Specified
In this example, the code uses os.listdir() to obtain a list of files and directories in the current working directory. It then prints the obtained list, providing information about the files and directories present in the current working directory. If no path is specified, it defaults to the current working directory.
Python3
# importing os module import os # os.listdir() method return path dir_list = os.listdir() print ( "Files and directories in current working directory :" ) # print the list print (dir_list) |
Output:
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']
Frequently Asked Questions (FAQs)
What do you understand by os.listdir() Method?
`os.listdir()` is a Python method used to obtain a list of files and directories in a specified path or the current working directory. If no path is provided then the method returns the list, allowing for easy exploration and manipulation of file system contents in a Python script.