C#(或python)递归文件目录并存在字典中,能提供思路不管什么语言都可以

想递归查找指定目录下全部文件和文件夹,并生成如下格式的字典数据格式

{
    "label":"文件夹1",
    "children":[
        {
            "label":"文件1"
        },
        {
            "label":"文件夹2",
            "children":[
              {"label":"文件2"}
             ]
        }
    ]
}

自己试了很久总会有问题,百度上面都是直接字符串打印到txt文件下。

阅读 2.3k
3 个回答

因为什么语言都可以,我这边提供一个java的吧。
思路就是创建一个目录类,里面有labelchildren两个字段,前者为名称,后者为List。
每次递归创建这个目录类,存放信息。

    public static void main(String[] args) {
        String path = "C:\\Users\\(----)\\Desktop\\新建文件夹";
        Catalog catalog = traversalDirectory(path, new Catalog());
        System.out.println(new Gson().toJson(catalog));
    }

    static Catalog traversalDirectory(String path, Catalog catalog) {
        File file = new File(path);
        if (file.exists()) {
            catalog.setLabel(file.getName());
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                return new Catalog();
            } else {
                for (File file2 : files) {
                    Catalog info = new Catalog();
                    if (file2.isDirectory()) {
                        traversalDirectory(file2.getAbsolutePath(), info);
                    }
                    info.setLabel(file2.getName());
                    catalog.getChildren().add(info);
                }
            }
        }
        return catalog;
    }

目录类:

    static class Catalog {
        private String label;
        private List<Catalog> children = new ArrayList<>();

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public List<Catalog> getChildren() {
            return children;
        }

        public void setChildren(List<Catalog> children) {
            this.children = children;
        }
    }

输出:

{"label":"新建文件夹","children":[{"label":"1","children":[]},{"label":"2","children":[{"label":"新建文本文档.txt","children":[]}]}]}
{
    "label":"新建文件夹",
    "children":[
        {
            "label":"1",
            "children":[

            ]
        },
        {
            "label":"2",
            "children":[
                {
                    "label":"新建文本文档.txt",
                    "children":[

                    ]
                }
            ]
        }
    ]
}
新手上路,请多包涵
        private static SelfDirInfo GetDirInfo(string path)
        {
            if (!Directory.Exists(path))
                return default;

            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            var result = GetDirInfo(directoryInfo);
            return result;
        }

        private static SelfDirInfo GetDirInfo(DirectoryInfo directoryInfo)
        {
            var result = new SelfDirInfo()
            {
                Lable = directoryInfo.Name,
                Childen = directoryInfo.GetDirectories()?.Select(p => GetDirInfo(new DirectoryInfo(p.FullName)))?.ToList()
            };
            return result;
        }

        class SelfDirInfo
        {
            public string Lable { get; set; }
            public List<SelfDirInfo> Childen { get; set; }
        }
        class SelfFileInfo
        {
            public string Label { get; set; }
        }

c#代码

   public static Catalog TraversalDirectory(string path, Catalog catalog)
    {
        DirectoryInfo file = new DirectoryInfo(path);

        if (file.Exists)
        {
            catalog.setLabel(file.Name);
            FileSystemInfo[] files = file.GetFileSystemInfos();
            if(files==null || files.Length == 0)
            {
                return new Catalog();
            }
            else
            {
                foreach(FileSystemInfo file2 in files)
                {
                    Catalog info = new Catalog();
                    if(Directory.Exists(file.FullName))
                    {
                        TraversalDirectory(file2.FullName, info);
                    }
                    info.setLabel(file2.Name);
                    catalog.getChildren().Add(info);
           
                }

            }

        }
        return catalog;
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题