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
Asked
Active
Viewed 47 times
0
-
It's impossible for 2 files to have the same name in the same folder – mousetail Jul 29 '22 at 11:26
-
@mousetail I meant to same in the sub-folders – Siddharth Chavan Jul 29 '22 at 11:28
-
See [here](https://stackoverflow.com/questions/50714469/recursively-iterate-through-all-subdirectories-using-pathlib) about recursively iteration – maskalev Jul 29 '22 at 12:59
-
It's unclear what you're asking. Perhaps give a example of what you have and what you'd expect. – pylang Mar 02 '23 at 23:21
1 Answers
0
Folder structure used
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)

Sai_MSP
- 129
- 4