The os.listdir()
method in Python gets the files and directories present in a given path.
import os
os.listdir(path)
We need to import the os
module to use the listdir()
method.
We provide path
as a parameter to this function. By default, it takes the present directory as a path.
This method returns the list
of files and directories in a given path.
Let us take a look at an example.
#Import the OS moduleimport os#Get files in the current directoryprint(os.listdir())
os
module. This comes with methods like mkdir
, listdir
, etc.listdir()
method.