0

How can I merge all the xlsx with a specific name in a given folder and subfolder. I mean it want it to recurse and find all the .xlsx with the same name

1 Answers1

0

Folder structure used

enter image description here

Check this out and check whether it works you!

import glob
from pprint import pprint

your_folder_path = os.path.relpath(os.getcwd())

def list_all_xlsx(path):
    return [files for files in os.listdir(path) if files[-4:]=="xlsx"]

filemap = {}
all_files = []

for path in glob.glob(f'{your_folder_path}') + glob.glob(f'{your_folder_path}/*/**/', recursive=True):
    filemap[path] = list_all_xlsx(path)
    all_files.extend(filemap[path])
unique_files = set(all_files)

print(unique_files)  #prints all the files found in that folder and its subfolders
print(filemap)    #dictionary holding files found in that folder and its subfolders

final_dict = {each:[] for each in unique_files}

for key, value in filemap.items():
    for each in unique_files:
        if each in value:
            final_dict[each].append(key)

for k,v in final_dict.items():
    if(len(v) > 1):   # condition to check a file found in multiple folders
        print("file", k, "found in these directories", v)

My output: enter image description here

Sai_MSP
  • 129
  • 4