简体   繁体   中英

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' contains a bunch of subdirectories which contain subsubdirectories which contain subsubsubdirectories and so on. 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. I also need to move all the files containing 'IC' to the top level subdirectories(see hashtag in code)

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 — Unix style pathname pattern expansion¶

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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