I am very new to Python, but I have read through the w3schools tutorial before starting out.
A recent web search led me to this helpful script which produces a JSON representation of a file tree.
#!/usr/bin/env python
import os
import errno
def path_hierarchy(path):
hierarchy = {
'type': 'folder',
'name': os.path.basename(path),
'path': path,
}
try:
hierarchy['children'] = [
>>> path_hierarchy(os.path.join(path, contents))
for contents in os.listdir(path)
]
except OSError as e:
if e.errno != errno.ENOTDIR:
raise
if os.path.basename(path).endswith('doc') or os.path.basename(path).endswith('docx'):
hierarchy['type'] = 'file'
else:
+++ hierarchy = None
return hierarchy
if __name__ == '__main__':
import json
import sys
try:
directory = sys.argv[1]
except IndexError:
directory = "/home/something/something"
print(json.dumps(path_hierarchy(directory), indent=4, sort_keys=True))
I have 2 questions :
At the position marked by ">>>", why doesn't the FOR statement precede the call to the method path_hierarchy?
How do I avoid adding a hierarchy object for a file which is neither "doc" or "docx"? I experimented with setting the hierarchy object to None at the line marked "+++" but this simply returned a "null" in the JSON output. What I would like is no entry at all unless the current item is a folder or a type allowed by my test (in this case either 'doc' or 'docx')