6
2

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 3 years have passed since last update.

あるディレクトリ以下のファイルツリーをJSONで再帰的に出力する

Last updated at Posted at 2016-04-02

やりたいこと

Bootstrap Tree View を使って、見栄えよくあるディレクトリ以下のファイル一覧を表示させたい。

誰か作っているだろうと思ったのですが、探しても見つからなかったので書きました。

#!/usr/bin/env python

import os
import json

def json_tree(path):
	target_json = []

	for item in os.listdir(path):
		new_hash = {}
		new_hash['text'] = item

		full_path = os.path.join(path,item)
		if os.path.isdir(full_path):
			child_dir = json_tree(full_path)
			new_hash['nodes'] = append(child_dir)
		else:
			new_hash['href'] = full_path.replace(os.getcwd(),'')

		target_json.append(new_hash)
	return target_json

if __name__ == '__main__':
	path = os.getcwd()+'/files'
	print(json.dumps(json_tree(path)))

ソースはこちらにも置いておきました。

6
2
0

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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?