简体   繁体   中英

Loop over files in a directory and merge them python

i have 2 files in a folder called looper:

testFile.csv 
file2.csv

I want my python code to loop over each file in the folder and merge them all into a single output file. So far, my code is as follows:

def combine_csv_files(input_folder_path, output_path):
    fout = open(output_path, "a")
    for file in sorted(os.listdir(input_folder_path)):
        for line in open(file):
            fout.write(line)

With this I am getting the error:

IOError: [Errno 2] No such file or directory: 'file2.csv'

I don't understand why. Also, when I merge these files, I only want to get the column headers for the first file. So I want to merge all the remaining files just form the second row. Please help!

os.listdir() returns only file names, to get the full path, use os.path.join() :

full_path = os.path.join(input_folder_path, file)
for line in open(full_path):
    fout.write(line)

As for your follow-up question about skipping the first line, the simplest way is to use itertools.islice :

from itertools import islice    
for line in islice(open(full_path), 1, None):
    fout.write(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM