csv - Loop over files in a directory and merge them python -
i have 2 files in folder called looper:
testfile.csv file2.csv
i want python code loop on each file in folder , merge them single output file. far, code follows:
def combine_csv_files(input_folder_path, output_path): fout = open(output_path, "a") file in sorted(os.listdir(input_folder_path)): line in open(file): fout.write(line)
with getting error:
ioerror: [errno 2] no such file or directory: 'file2.csv'
i don't understand why. also, when merge these files, want column headers first file. want merge remaining files form second row. please help!
os.listdir()
returns file names, full path, use os.path.join()
:
full_path = os.path.join(input_folder_path, file) line in open(full_path): fout.write(line)
as follow-up question skipping first line, simplest way use itertools.islice
:
from itertools import islice line in islice(open(full_path), 1, none): fout.write(line)
Comments
Post a Comment