繁体   English   中英

Python3 从文件 txt 文件目录列表创建 JSON

[英]Python3 creating JSON from file txt file directory listing

我希望将文件目录列表转换为一些结构化数据。

典型的列表如下所示。

test
|_ animals
|___ cats
|______images
|________ cat1.jpg
|________ cat2.jpg
|______textfile.txt
    C:\test>dir /s | more
 Volume in drive C has no label.
 Volume Serial Number is 0036-34A6

 Directory of C:\test

01-Mar-20  04:25 PM    <DIR>          .
01-Mar-20  04:25 PM    <DIR>          ..
01-Mar-20  04:25 PM    <DIR>          animals
               0 File(s)              0 bytes

 Directory of C:\test\animals

01-Mar-20  04:25 PM    <DIR>          .
01-Mar-20  04:25 PM    <DIR>          ..
01-Mar-20  04:26 PM    <DIR>          cats
               0 File(s)              0 bytes

 Directory of C:\test\animals\cats

01-Mar-20  04:26 PM    <DIR>          .
01-Mar-20  04:26 PM    <DIR>          ..
01-Mar-20  04:26 PM    <DIR>          images
01-Mar-20  04:26 PM                 0 textfile.txt
               1 File(s)              0 bytes

 Directory of C:\test\animals\cats\images

01-Mar-20  04:26 PM    <DIR>          .
01-Mar-20  04:26 PM    <DIR>          ..
01-Mar-20  04:26 PM                 0 cat1.jpg
01-Mar-20  04:26 PM                 0 cat2.jpg
               2 File(s)              0 bytes

     Total Files Listed:
               3 File(s)              0 bytes
              11 Dir(s)  94,860,820,480 bytes free

我已经构建了代码来遍历这些行并将其保存到 csv 中,但我想将其保存到 json 中,以便我可以保留层次结构。

最后,我希望 json 看起来像,

{
  "data": [
    {
      "type": "Folder",
      "name": "animals",
      "path": "c:\\test\\animals",
      "children": [
        {
          "type": "folder",
          "name": "cats",
          "path": "c:\\test\\animals\\cats\\",
          "children": [
            {
              "type": "folder",
              "name": "images",
              "path": "c:\\test\\animals\\cats\\images\\",
              "children": [
                {
                  "type": "file",
                  "name": "cat1.jpg",
                  "filetype": "jpg",
                  "size": "0",
                  "path": "c:\\test\\animals\\cats\\images\\cat1.jpg"
                },
                {
                  "type": "file",
                  "name": "cat2.jpg",
                  "size": "0",
                  "filetype": "jpg",
                  "path": "c:\\test\\animals\\cats\\images\\cat2.jpg"
                }
              ]
            },
            {
              "type": "file",
              "name": "textfile.txt",
              "filetype": "txt",
              "size": "0",
              "path": "c:\\test\\animals\\cats\\textfile.txt"
            }
          ]
        }
      ]
    }
  ]
}

有没有一种简单的方法可以做到这一点?

或者我是否需要通过遍历行来手动构建层次结构? 我会假设必须有一些python库来处理这个问题?

提前致谢

您可以使用递归递归遍历给定目录。 为此,您可以使用 python os的内置模块,它提供了各种文件系统信息和操作方法。 要将 json 数据写入文件,您可以使用内置的json模块。 这可以通过以下代码实现:

import os
import json

source = "your_source_dir_path" # --> path to the source dir

def dumptree(source):
    info_list = []
    for filename in os.listdir(source):
        info = dict()

        fullpath = os.path.join(source, filename)
        if os.path.isfile(fullpath):
            info["type"] = "file"
            info["name"] = filename
            info["filetype"] = os.path.splitext(filename)[-1]
            info["size"] = os.path.getsize(fullpath)
            info["path"] = fullpath
        else:
            info["type"] = "folder"
            info["name"] = filename
            info["path"] = fullpath
            info["children"] = dumptree(fullpath)

        info_list.append(info)

    return info_list

data = {}
data["data"] = dumptree(source)
with open("output_filename.json", "w") as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM