Last active
August 13, 2023 15:16
-
-
Save PlaceReporter99/847eff234e485a93a00c3aba1970a334 to your computer and use it in GitHub Desktop.
Takes a file path, scans it, and returns a python dictionary representing it.
This file contains hidden or 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
def dirtodict(path: str = os.getcwd(), encoding: str = 'utf-8') -> dict: | |
temp = {} | |
directory = set(map(lambda x:os.path.join(path, x), os.listdir(path))) | |
files = set(map(lambda x:x.replace("\\\\","\\"), filter(os.path.isfile, map(lambda x:os.path.join(path, x), set(os.listdir(path)))))) | |
folders = directory - files | |
for file in files: | |
with open(file, encoding = encoding) as f: | |
temp.update({os.path.basename(file): f.read()}) | |
for folder in folders: | |
temp.update({os.path.basename(folder): dirtodict(os.path.join(path, folder), encoding)}) | |
return temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment