Python How to list all files in a directory?
By:Roy.LiuLast updated:2019-08-11
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
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
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\ 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
From:一号门
COMMENTS