Created
August 27, 2019 17:36
-
-
Save brydavis/22a913738e4317c74eae285abb4536d3 to your computer and use it in GitHub Desktop.
Finding files within subfolders without using `os.walk` or `glob` libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pprint import pprint | |
data = {} | |
def find_files(folder, file_extension): | |
for item in os.listdir(folder): | |
item = "/".join([folder, item]) | |
if os.path.isdir(item): | |
data[item] = [] | |
find_files(item, file_extension) # RECURSION TIME | |
if len(data[item]) == 0: | |
del data[item] | |
else: | |
if item[-1*len(file_extension):] == file_extension: | |
# print(item) | |
basename = os.path.basename(item) | |
dirname = os.path.dirname(item).strip("/") | |
data[dirname].append(basename) | |
find_files(".", ".png") | |
pprint(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Python class, here's what final structure should look like.