Home > Software engineering >  How to get the full file path including the directory?
How to get the full file path including the directory?

Time:11-11

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

CodePudding user response:

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]))
  • Related