Skip to content

Instantly share code, notes, and snippets.

@PlaceReporter99
Last active August 13, 2023 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PlaceReporter99/847eff234e485a93a00c3aba1970a334 to your computer and use it in GitHub Desktop.
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.
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