0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonでディレクトリを読み込んで階層をJSON化。Directory to json with Python.

Posted at

Pythonでディレクトリを読み込んで階層をJSON化

Directory to json with Python.

このような構造のディレクトリを読み込んで、

D:\DIR
│  file01.txt
│  file02.txt
│
├─dir01
│  │  file01.txt
│  │  file02.txt
│  │  file03.txt
│  │
│  └─dir01
│          file01.txt
│
└─dir02
        file01.txt
        file02.txt

以下のようなJSONにします。

json
[
    {
        "label": "dir01",
        "path": "D:\\dir\\dir01",
        "children": [
            {
                "label": "dir01",
                "path": "D:\\dir\\dir01\\dir01",
                "children": [
                    {
                        "label": "file01.txt",
                        "path": "D:\\dir\\dir01\\dir01\\file01.txt"
                    }
                ]
            },
            {
                "label": "file01.txt",
                "path": "D:\\dir\\dir01\\file01.txt"
            },
            {
                "label": "file02.txt",
                "path": "D:\\dir\\dir01\\file02.txt"
            },
            {
                "label": "file03.txt",
                "path": "D:\\dir\\dir01\\file03.txt"
            }
        ]
    },
    {
        "label": "dir02",
        "path": "D:\\dir\\dir02",
        "children": [
            {
                "label": "file01.txt",
                "path": "D:\\dir\\dir02\\file01.txt"
            },
            {
                "label": "file02.txt",
                "path": "D:\\dir\\dir02\\file02.txt"
            }
        ]
    },
    {
        "label": "file01.txt",
        "path": "D:\\dir\\file01.txt"
    },
    {
        "label": "file02.txt",
        "path": "D:\\dir\\file02.txt"
    }
]
Python
import os
import re

targetDir="D:\\dir"
_count =0
def dirToJson(path) :
    global _count
    array = []
    files = os.listdir(path)
    files = sorted( files )
    for f in files:
        if f[0] !='.': #ドットで始まるファイルは読み込まない
            _count=_count+1
            fp=os.path.join(path, f)
            array.append('{"label":"' + f + '"')
            array.append('"path":"' + re.sub(r'\\', "\\\\\\\\",fp) + '"')
            if  not os.path.isfile(fp):
                fp=re.sub(r'\\', "\\\\",fp)
                array.append('"children":[' + dirToJson(fp) +']')
            array.append("}")
            
            if (_count >= 1000) : #読み込み制限
                break

    result = ",".join(array)
    return  re.sub(r',}', "}", result)
                 
result=dirToJson(targetDir)
jsonDir = "["+result+"]"

print(jsonDir)

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?