0

This is a doubt on an existing or closed thread, however I was disabled from adding a comment that's why this. I was checking out this code and wondering how this piece is carefully getting rid of headers from other files while merging ? I do not see any element in the code that is set to ignore the headers from the files. Code that I'm referring to:

Concatenating multiple csv files into a single csv with the same header - Python

import shutil
import glob


#import csv files from folder
path = r'data/US/market/merged_data'
allFiles = glob.glob(path + "/*.csv")
allFiles.sort()  # glob lacks reliable ordering, so impose your own if output order matters
with open('someoutputfile.csv', 'wb') as outfile:
    for i, fname in enumerate(allFiles):
        with open(fname, 'rb') as infile:
            if i != 0:
                infile.readline()  # Throw away header on all but first file
            # Block copy rest of file from input to output without parsing
            shutil.copyfileobj(infile, outfile)
            print(fname + " has been imported.")
6
  • 2
    There's a comment which says "# Throw away header on all but first file"! And that's what it does. Commented Jan 17, 2021 at 4:45
  • 2
    "I'm new to this world." Welcome to Earth then! Did you read the comments in the code? Commented Jan 17, 2021 at 4:45
  • 1
    infile.readline() effectively discards the line so that shutil.copyfileobj will no longer read it when reading from infile. This happens on all files except for the first (hence the i != 0, so that the output file will only have the header from the first file. Commented Jan 17, 2021 at 4:47
  • see how python file handling works with seek and readline and such Commented Jan 17, 2021 at 4:47
  • agreed! but the function of readline is to read the contents of the file or other similar purpose. How is it helping in getting rid of the headers? Commented Jan 17, 2021 at 4:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.