文件夹结构到json与python(unicode)

时间:2016-06-13 15:45:09

标签: python json struct io

该程序在json中创建文件夹/文件结构     #!/ usr / bin / env python

a[href*="#"]:not([href="#"])

它实际上是有效的,但它无法使其适用于希腊和日本的角色。这是文件的输出。它是一个编码问题,还是python中的json类。我使用python3所以一切都应该是unicode但仍然......

import os
import errno
import json
import sys


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
       hierarchy['type'] = os.path.splitext(path)[1]
       if hierarchy['type'] == "":
           hierarchy['type'] = "Unknown"
   return hierarchy

if __name__ == '__main__':


try:
    directory = sys.argv[1]
except IndexError:
    directory = os.getcwd()

fo = open("output.json", "w")
fo.write(json.dumps(path_hierarchy(directory), indent=2, sort_keys=False,))
fo.close()

0 个答案:

没有答案