简体   繁体   English

构建我的目录和文件路径的字典 select 所有名称包含特定字符串的文件

[英]building a dictionary of my directories and file paths to select all files whose name contains a specific string

I have a rootdirectory called 'IC'.我有一个名为“IC”的根目录。 'IC' contains a bunch of subdirectories which contain subsubdirectories which contain subsubsubdirectories and so on. “IC”包含一堆子目录,这些子目录包含子目录,子目录包含子子目录等等。 Is there an easy way to move all the sub...directory files into their parent subdirectory and then delete the empty sub...directories.有没有一种简单的方法可以将所有子...目录文件移动到它们的父子目录中,然后删除空的子...目录。

So far I've made this monstrosity of nested loops to build a dictionary of file paths and subdirectories as dictionaries containing file paths etc. I was gonna then make something to go through the dictionary and pick all files containing 'IC' and the subdirectory they are in. I need to know which directories contain an 'IC' file or not.到目前为止,我已经制作了这种嵌套循环的怪物来构建文件路径和子目录的字典作为包含文件路径等的字典。然后我将通过字典对 go 做一些事情,然后选择所有包含“IC”的文件和它们的子目录在。我需要知道哪些目录包含“IC”文件。 I also need to move all the files containing 'IC' to the top level subdirectories(see hashtag in code)我还需要将所有包含“IC”的文件移动到顶级子目录(参见代码中的标签)

import os, shutil

rootdir = 'data/ICs'

def dir_tree(rootdir):
    IC_details = {}
# This first loop is what I'm calling the top level subdirectories. They are the three
# subdirectories inside the directory 'data/ICs'
    for i in os.scandir(rootdir):
        if os.path.isdir(i):
            IC_details[i.path] = {}
    for i in IC_details:
        for j in os.scandir(i):
            if os.path.isdir(j.path):
                IC_details[i][j.name] = {}
            elif os.path.isfile(j.path):
                IC_details[i][j.name] = [j.path]
        for j in IC_details[i]:
            if os.path.isdir(os.path.join(i,j)):
                for k in os.scandir(os.path.join(i,j)):
                    if os.path.isdir(k.path):
                        IC_details[i][j][k.name] = {}
                    elif os.path.isfile(k.path):
                        IC_details[i][j][k.name] = [k.path]
                for k in IC_details[i][j]:
                    if os.path.isdir(os.path.join(i,j,k)):
                        for l in os.scandir(os.path.join(i,j,k)):
                            if os.path.isdir(l.path):
                                IC_details[i][j][k][l.name] = {}
                            elif os.path.isfile(l.path):
                                IC_details[i][j][k][l.name] = [l.path]
                        for l in IC_details[i][j][k]:
                            if os.path.isdir(os.path.join(i,j,k,l)):
                                for m in os.scandir(os.path.join(i,j,k,l)):
                                    if os.path.isfile(m.path):
                                        IC_details[i][j][k][l][m.name] = [m.path]
    return IC_details

IC_tree = dir_tree(rootdir)

You should have a look at the 'glob' module:你应该看看'glob'模块:

glob — Unix style pathname pattern expansion¶ glob — Unix 样式路径名模式扩展¶

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

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