Last active
July 2, 2024 14:06
-
-
Save fulcrum6378/d86c04df85aeec6a12aafa2bf690839c to your computer and use it in GitHub Desktop.
Map all files in a path recursively into an XML file
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 | |
import dict2xml | |
def folder(path): | |
global root | |
children = dict() | |
try: | |
mass = os.listdir(path) | |
except PermissionError: | |
return children | |
for x in mass: | |
item = os.path.join(path, x) | |
if os.path.isdir(item): | |
try: | |
children[x] = folder(item) | |
except RecursionError: | |
children[x] = None | |
elif os.path.isfile(item): | |
children[x] = None | |
else: | |
children[x] = None | |
return {os.path.basename(path): children} if path == root else children | |
root = os.path.abspath(os.path.join(__file__, os.pardir)) | |
with open(os.path.join(root, "map.xml"), "w") as f: | |
f.write(dict2xml.Converter("").build(folder(root), closed_tags_for=[None])) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment