Python 生成目录 json 信息
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import argparse
import json
import time
def generate_directory_tree(start_path):
dir_tree = []
for item in os.listdir(start_path):
item_path = os.path.join(start_path, item)
item_info = {
'name': item,
'updated_at': time.ctime(os.path.getctime(item_path)),
}
if os.path.isdir(item_path):
item_info['children'] = generate_directory_tree(item_path)
else:
item_info['children'] = None
dir_tree.append(item_info)
return dir_tree
def main():
parser = argparse.ArgumentParser(description='Generate a directory tree as a JSON file.')
parser.add_argument('input_directory', help='Path to the input directory')
parser.add_argument('output_file', help='Path to the output JSON file')
args = parser.parse_args()
directory_tree = generate_directory_tree(args.input_directory)
with open(args.output_file, 'w') as f:
json.dump(directory_tree, f, indent=4)
if __name__ == '__main__':
main()

评论栏
分享你的想法,或参与下面的讨论。
参与讨论
支持 Markdown、代码和图片
友善交流,让讨论更有价值。
更多留言
加载中…