Hello
I have python script just like this
#!/bin/python
import os
import json
import datetime
HOMEPATH = '/opt/monitor_dirs/SomeDir'
def path_to_dict(path, depth = 1, first = False):
for base, dirs, files in os.walk(path):
r = {'name': base, 'dirs': len(dirs), 'files': len(files)}
if first:
r['datetime'] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S%z")
if depth > 0:
r['subdirs'] = {}
for subdir in dirs:
r['subdirs'][subdir] = path_to_dict(os.path.join(path, subdir), depth - 1);
return r
#print path_to_dict(HOMEPATH, 1)
result = path_to_dict(HOMEPATH, 1, True)
if result:
print (json.dumps(result, sort_keys=True, indent=4))
And i have output
# ./file_count.py
{
"datetime": "2023-02-22T21:10:49",
"dirs": 9,
"files": 0,
"name": "/opt/monitor_dirs/SomeDir",
"subdirs": {
"XXXX": {
"dirs": 0,
"files": 63,
"name": "/opt/monitor_dirs/XXXX"
}
}
}
There is some problem in Index
I have 2 event instead just only one
1. {
2. ""datetime": "2023-02-22T21:10:49", "dirs": 9, "files": 0, and so on, but there is no '{'
How i can get only one event with my JSON
... View more