2

I'm trying to merge content from different TXT files by using Python, but the challenge is I need to only merge content from the same file names coming from different folders. Here's a screenshot for your reference:

enter image description here

So far, I can print out all the file names with their full paths:

import os

for root, dirs, files in os.walk(".", topdown=False):
    for file in files:
        if file.endswith(".txt"):
            filepath = os.path.join(root, file)
            print (filepath)

However, how could I use Python to only merge files with the same name...still researching. Let me know if you know the answer, or point me a way to research more. Thank you very much and happy holidays!

Penny
  • 1,218
  • 1
  • 13
  • 32

2 Answers2

3
import os

# create a dictionary with file names as keys
# and for each file name the paths where they
# were found
file_paths = {}
for root, dirs, files in os.walk('.'):
    for f in files:
        if f.endswith('.txt'):
            if f not in file_paths:
                file_paths[f] = []
            file_paths[f].append(root)

# for each file in the dictionary, concatenate
# the content of the files in each directory
# and write the merged content into a file
# with the same name at the top directory
for f, paths in file_paths.items():
    txt = []
    for p in paths:
        with open(os.path.join(p, f)) as f2:
            txt.append(f2.read())
    with open(f, 'w') as f3:
        f3.write(''.join(txt))
David Brochart
  • 825
  • 9
  • 17
-1

You should do something like below, note: code not tested.

import os

mapped_files = {}

for path, subdirs, files in os.walk("."):
    for file in files:
        if file.endswith(".txt"):
            if file in mapped_files:
                existing = mapped_files[file]
                mapped_files[file] = existing.append(path)
            else:
                mapped_files[file] = [path]

for key in mapped_files:
    files = mapped_files[key]
    first_f = os.path.join(path, files[0])
    with open(first_f, "a+") as current_file: 
        for path in files[1:]: # start at the second index
            f = os.path.join(path, key)
            content = open(f,"r").read()
            current_file.write(content) # add all content to the first file
johnII
  • 1,423
  • 1
  • 14
  • 20