Skip to content

Instantly share code, notes, and snippets.

@fulcrum6378
Last active July 2, 2024 14:06
Show Gist options
  • Save fulcrum6378/d86c04df85aeec6a12aafa2bf690839c to your computer and use it in GitHub Desktop.
Save fulcrum6378/d86c04df85aeec6a12aafa2bf690839c to your computer and use it in GitHub Desktop.
Map all files in a path recursively into an XML file
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