
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I list all files of a directory in Python?
os.listdir(my_path) will get you everything that's in the my_path directory - files and directories.
Example
You can use it as follows:
>>> import os >>> os.listdir('.') ['DLLs', 'Doc', 'etc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'Scripts', 'share', 'tcl', 'Tools', 'w9xpopen.exe']
Output
If you want just files, you can filter it using isfile:
>>> import os >>> file_list = [f for f in os.listdir('.') if os.path.isfile(os.path.join('.', f))] >>> print file_list ['LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'w9xpopen.exe']
- Related Articles
- How to list all files in a directory using Java?
- How to delete all files in a directory with Python?
- How to list all files (only) from a directory using Java?
- Java program to List all files in a directory recursively
- How can I get the list of files in a directory using C/C++?
- How to list down all the files available in a directory using C#?
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- How can I iterate over files in a given directory in Python?
- How can I get the list of files in a directory using C or C++?
- C Program to list all files and sub-directories in a directory
- How can I list the contents of a directory in Python?
- How to find all files in a directory with extension .txt in Python?
- How do I get a list of all instances of a given class in Python?
- How to unzip all zipped files in a Linux directory?
- How do I get the parent directory in Python?

Advertisements