7/17/2019 Python – How to list all files in a directory? – Mkyong.
com
All Tutorials
Java 8
Spring Boot
Maven
JSON
Contact Me
Java Events 2019
Search for... Search
Python – How to list all files in a directory?
By mkyong | December 24, 2018 | Viewed : 96,881 | +6,511 pv/w
In Python, we can use os.walker or glob to create a find() like function to search or list files or folders in a
specified directory and also it’s subdirectories.
1. os.walker
1.1 List all .txt files in a specified directory + subdirectories.
import os
path = 'c:\\projects\\hc2\\'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.txt' in file:
files.append(os.path.join(r, file))
for f in files:
print(f)
Output
https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/ 1/6
7/17/2019 Python – How to list all files in a directory? – Mkyong.com
c:\projects\hc2\app\readme.txt
c:\projects\hc2\app\release.txt
c:\projects\hc2\web\readme.txt
c:\projects\hc2\whois\download\afrinic.txt
c:\projects\hc2\whois\download\apnic.txt
c:\projects\hc2\whois\download\arin.txt
c:\projects\hc2\whois\download\lacnic.txt
c:\projects\hc2\whois\download\ripe.txt
c:\projects\hc2\whois\out\test\resources\asn\afrinic\3068.txt
c:\projects\hc2\whois\out\test\resources\asn\afrinic\37018.txt
//...
1.2 List all directories in a specified directory + subdirectories.
import os
path = 'c:\\projects\\hc2\\'
folders = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for folder in d:
folders.append(os.path.join(r, folder))
for f in folders:
print(f)
Output
c:\projects\hc2\
c:\projects\hc2\analyzer\
c:\projects\hc2\analyzer\out\
c:\projects\hc2\analyzer\out\production\
c:\projects\hc2\analyzer\out\production\classes\
c:\projects\hc2\analyzer\out\production\classes\com\
//...
2. glob
https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/ 2/6
7/17/2019 Python – How to list all files in a directory? – Mkyong.com
Changed in version 3.5: Support for recursive globs using **.
2.1 List all .txt files in a specified directory + subdirectories (**).
import glob
path = 'c:\\projects\\hc2\\'
files = [f for f in glob.glob(path + "**/*.txt", recursive=True)]
for f in files:
print(f)
Output
c:\projects\hc2\app\readme.txt
c:\projects\hc2\app\release.txt
c:\projects\hc2\web\readme.txt
c:\projects\hc2\whois\download\afrinic.txt
c:\projects\hc2\whois\download\apnic.txt
c:\projects\hc2\whois\download\arin.txt
c:\projects\hc2\whois\download\lacnic.txt
c:\projects\hc2\whois\download\ripe.txt
c:\projects\hc2\whois\out\test\resources\asn\afrinic\3068.txt
c:\projects\hc2\whois\out\test\resources\asn\afrinic\37018.txt
//...
2.2 List all directories in a specified directory + subdirectories (**).
import glob
folders = [f for f in glob.glob(path + "**/", recursive=True)]
for f in folders:
print(f)
Output
c:\projects\hc2\
c:\projects\hc2\analyzer\
https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/ 3/6
7/17/2019 Python – How to list all files in a directory? – Mkyong.com
c:\projects\hc2\analyzer\out\
c:\projects\hc2\analyzer\out\production\
c:\projects\hc2\analyzer\out\production\classes\
c:\projects\hc2\analyzer\out\production\classes\com\
//...
References
1. Pyhton docs – glob
2. Python docs – os.walker
glob io os.walker python
About the Author
author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials,
consider make a donation to these charities.
Comments
avatar Join the discussion...
2 2 0 4
Puneet
HeloldanielCharles
newest oldest most voted
daniel
daniel
Guest
Your article helped me!
Thanks a lot!
3 Reply 1 month ago
https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/ 4/6
7/17/2019 Python – How to list all files in a directory? – Mkyong.com
Charles
Charles
Guest
Do you know of any way to carry out the task of listing all files in a web directory? I am trying to do a mass
download of images from a web directory. Do you know how to get the list of names or would you suggest a
method to download all of the contents of a web directory from a server? Thanks
0 Reply 2 months ago
Helol Helol
Guest
hi
0 Reply 29 days ago
Puneet Puneet
Guest
You should try web scraping , there are lot of good library in python , which you can use for this, Let
me know what is the exact need and i can share some code or some ideas
0 Reply 28 days ago
Favorites Links
{To be update...}
Partners & Bookmarks
JAX London
Java Code Geeks
About Mkyong.com
Mkyong.com is for Java and J2EE developers, all examples are simple and easy to understand, and well tested in
our development environment.
Mkyong.com is created, written by, and maintained by Yong Mook Kim, aka Mkyong.
https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/ 5/6
7/17/2019 Python – How to list all files in a directory? – Mkyong.com
Copyright © 2008-2019 Mkyong.com, all rights reserved. Privacy Policy
https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/ 6/6