0

I have a quiet complex problem. I have multiple filenames in a list, the root directory of those files is the same: mother_directory. However every file has a different subdirectory. Now I have a script which is processing some files and I need to know the exact full path including the subdirectories of every file. I know that I could use os.walk but that will make my function too nested as inside this function I'm planning to use another function which uses those full paths.

This is the file structure:

mother_directory:
               |_child1:
                      20211011.xml
                      20211001.xml
               |_child2:
                      20211002.xml

This is my current code:

mother_path = r'c:\data\user1\Desktop\mother_directory'

blue_dates = ['20211011', '20211012', '20211013', '20211001', '20211002']
red_dates =  ['20211011', '20211009', '20211008', '20211001', '20211002']
file_names = ['20211011.xml', '20211001.xml', '20211002.xml']

def process_files(x):
  if x in red_dates:
    match_file = [s for s in file_names if x in s] 
    file_path = os.path.join(mother_path, match_file [0])
    print(file_path)

for x in blue_dates:
  process_files(x)

My current output:

c:\data\user1\Desktop\mother_directory\20211011.xml
c:\data\user1\Desktop\mother_directory\20211001.xml
c:\data\user1\Desktop\mother_directory\20211002.xml

When I run my function I want my desired output to be like this:

c:\data\user1\Desktop\mother_directory\child1\20211011.xml
c:\data\user1\Desktop\mother_directory\child1\20211001.xml
c:\data\user1\Desktop\mother_directory\child2\20211002.xml
martineau
  • 119,623
  • 25
  • 170
  • 301
Al-Andalus
  • 115
  • 1
  • 8
  • I believe you'll find your answer here. [Link](https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) – alphaBetaGamma Nov 10 '21 at 09:07
  • @alphaBetaGamma I checked that link out already, done that but I constantly get `child1` as directory while my last file is placed in `child2` – Al-Andalus Nov 10 '21 at 09:09
  • What do you mean using `os.walk` would make the function too nested? – martineau Nov 10 '21 at 09:23
  • @martineau if I use os.walk I will need to use a loop and when I print file_path then I get the file_path printed to many times – Al-Andalus Nov 10 '21 at 09:25

1 Answers1

1

I added a condition, I believe it will work now.

def process_files(x):
    if x in red_dates:
        match_file = [s for s in file_names if x in s]
        for root, dirs, files in os.walk(mother_path):
            for file in files:
                if match_file[0] in file:
                    print(os.path.join(root,match_file[0]))
alphaBetaGamma
  • 653
  • 6
  • 19