I have three CSV files each for a particular filename for multiple files. Let's say there are a total 20 filenames so total 20* 3csv files in three different folders.
Folder A- 1001.CSV,1002.CSV,1003.CSV...
Folder B-1001.CSV,1002.CSV,1003.CSV
Folder C-1001.csv,1002.csv,1003.csv......
I want to get a single CSV file for each 1001,1002,1003,1004..... So total 20csv files
How can I do this? Since the files are in different folders glob is not working(or I don't know how to)
I made the following assumptions:
This should produce a "concat.csv" file in each subfolder with the contents of all the other files in that same folder. I used a snippet of code from this other answer on stackoverflow for actually concatenating the files.
import os
import fileinput
rootdir = 'C:\\Users\\myname\\Desktop\\parentdir'
os.chdir(rootdir)
children = os.listdir()
for i in children:
path = os.path.join(rootdir, i)
os.chdir(path)
filenames = os.listdir()
with open('concat.csv', 'w') as fout, fileinput.input(filenames) as fin:
for line in fin:
fout.write(line + '\n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With