簡體   English   中英

獲取文件夾和文件的JSON樹(但僅包含包含給定字符串的文件)

[英]Getting JSON tree of folders and files (but only with files containing given string)

我需要獲取一種JSON格式的經過過濾的目錄/文件結構。

具體來說,我只需要包含包含給定字符串的文件,並且僅包含包含此類文件的目錄(本身或某些后代)。

這段代碼:

import os
import json

def path_to_dict(path):
    d = {'name': os.path.basename(path)}
    if os.path.isdir(path):
        d['type'] = "directory"
        d['children'] = [path_to_dict(os.path.join(path,x)) for x in os.listdir\
(path)]
    else:
        d['type'] = "file"
    return d

print json.dumps(path_to_dict('.'), indent=2)

從我當前目錄開始,以我想要的格式為我提供了所有目錄和文件的漂亮JSON樹:

{
    "type": "directory",
    "name": ".",
    "children": [
    {
      "type": "file", 
      "name": "attribute_container.c"
    }, 
    {
      "type": "file", 
      "name": "node.c"
    }, 
    {
      "type": "directory", 
      "name": "power", 
      "children": [
        {
          "type": "file", 
          "name": "clock_ops.c"
        }, 
        {
          "type": "file", 
          "name": "common.c"
        }, 
        {
          "type": "file", 
          "name": "domain.c"
        }, 
        {
          "type": "file", 
          "name": "domain_governor.c"
        }, 
        {
          "type": "file", 
          "name": "generic_ops.c"
        }, 
        {
          "type": "file", 
          "name": "wakeup.c"
        }
      ]
    }, 
    {
      "type": "directory", 
      "name": "regmap", 
      "children": [
        {
          "type": "file", 
          "name": "internal.h"
        }, 
        {
          "type": "file", 
          "name": "Kconfig"
        }, 
        {
          "type": "file", 
          "name": "Makefile"
        }, 
        {
          "type": "file", 
          "name": "regcache-flat.c"
        }, 
        {
          "type": "file", 
          "name": "regmap-spmi.c"
        }, 
        {
          "type": "file", 
          "name": "regmap.c"
        }
      ]
    }, 
    {
      "type": "file", 
      "name": "soc.c"
    }, 
    {
      "type": "file", 
      "name": "syscore.c"
    }, 
    {
      "type": "file", 
      "name": "topology.c"
    }, 
    {
      "type": "file", 
      "name": "transport_class.c"
    }   ] }

但是,我只需要包含給定字符串的文件。 此外,僅包含一個或多個此類文件的文件夾或其某些后代包含此文件。 (可以這么說,我需要某種“修剪”)

我知道在文件中找到字符串的解決方案:

my_file = ...
my_string = ...
infile = open(my_file,"r")

numlines = 0
found = 0
for line in infile:
    numlines += 1
    found += line.count(my_string)
infile.close()

print "%s was found %i times in %i lines", %string, %found, %numlines

但是我很難從問題的頂部將其集成到代碼中。

我感謝任何提示或建議。

我不想使用os.walk()重寫您的代碼。我將對您的代碼做一些小的更改。

關鍵是使用“無”作為刪除文件的前哨值,並使用空的children列表刪除目錄。 該實現編寫得不好,但是向您展示了如何使用測試的核心。

import os
import json

def check_in_file(my_file,my_string):
    with open(my_file) as f:
        try:
            return my_string in f.read()
        except:
            return False

def path_to_dict(path, my_string=None):
    d = {'name': os.path.basename(path)}
    if os.path.isdir(path):
        d['type'] = "directory"
        d['children'] = []
        paths = [os.path.join(path,x) for x in os.listdir(path)]
        #Just the children that contains at least a valid file
        for p in paths:
            c = path_to_dict(p, my_string)
            if c is not None:
                d['children'].append(c)
        if not d['children']:
            return None
    else:
        if my_string is not None and not check_in_file(path,my_string):
            return None
        d['type'] = "file"
    return d

print(json.dumps(path_to_dict('.',), indent=2))
print(json.dumps(path_to_dict('.','kkkkk'), indent=2))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM