I'm trying to merge content from different .dat files using Python. These files all have the same name:
taueq_polymer_substratechain1600_12_raf2_000_B0_20_S5_0.dat
but are inside different folders which contains other .dat files.
The content of the files is the following form: File Content ( two columns).
I am trying to merge all of these files into a single text file where every every two columns will be next to each other. Something similar to this:Desired Output but in text file.
I found some help here: How to merge content from files with same name but in different folders by Python?
However using this code:
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.startswith('taueq_polymer'):
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))
The output text file append the data of the files at the bottom of the original file and not next to it. Can anyone tell me how to stack the columns next to each other?
Thank you