Python os.listdir() Method
Example
Print a list of all entries in the current directory:
#Import os Library
import os
#print all entries in current directory
print (os.listdir())
Try it Yourself »
Definition and Usage
The os.listdir() method returns a list of the names of the entries in
a directory.
The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Note: Available on UNIX and WINDOWS platforms.
Syntax
os.listdir(path)
Parameter Values
| Parameter | Description |
|---|---|
| path | Optional. Specifies the directory to explore. If omitted, then list of files and directories in the current working directory is considered |
Technical Details
| Return Value: | A list value, representing the names of the entries in the directory
|
|---|---|
| Python Version: | pre 2.6 |
| Change Log: | 3.2 - The path parameter became optional 3.3 - Added support for specifying path as an open file descriptor 3.6 - Accepts a path-like object |