The listdir method of the os module
The listdir method returns a list of files in a folder. In the method parameter, we specify the path to the desired folder.
Syntax
import os
os.listdir(path to folder)
Example
Let's find out the list of files in a given folder:
import os
print(os.listdir('dir'))
Example
Let's find out the list of files in our working folder:
import os
print(os.listdir('.'))
See also
-
method
getcwdmoduleos,
which returns the current working directory -
method
makedirsof moduleos,
which creates folders -
method
mkdirof moduleos,
which creates one folder -
method
path.existsof moduleos,
which checks the existence of a path -
method
scandirof moduleos,
which iterates through the files in a folder -
method
statof moduleos,
which returns the status of the path