- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I iterate over files in a given directory in Python?
os.listdir(my_path) will get you everything that's in the my_path directory - files and directories. 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']
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']
- How do I list all files of a directory in Python?
- Iterate over a dictionary in Python
- Iterate over a list in Python
- Iterate over a set in Python
- How to iterate over a list of files with spaces in Linux?
- How can I get the list of files in a directory using C/C++?
- How can I get the list of files in a directory using C or C++?
- Iterate over characters of a string in Python
- How can I list the contents of a directory in Python?
- How to rename multiple files in a directory in Python?
- How to delete multiple files in a directory in Python?
- How can I iterate through two lists in parallel in Python?
- How to delete all files in a directory with Python?
- How to iterate over a Hashmap in Kotlin?
- How to iterate over a list in Java?