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.")
a121
  • 807
  • 4
  • 8
  • 18
New_here
  • 95
  • 6
  • 2
    There's a comment which says "# Throw away header on all but first file"! And that's what it does. – rici Jan 17 '21 at 04:45
  • 2
    *"I'm new to this world."* Welcome to Earth then! Did you read the comments in the code? – Klaus D. Jan 17 '21 at 04: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. – Alex Jan 17 '21 at 04:47
  • see how python file handling works with seek and readline and such – qwr Jan 17 '21 at 04: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? – New_here Jan 17 '21 at 04:47
  • Are you aware that the code calls the readline method, not the readline**s** method? – MisterMiyagi Jan 17 '21 at 12:20

0 Answers0