Skip to content

Instantly share code, notes, and snippets.

@icedraco
Created February 19, 2016 10:08
Show Gist options
  • Select an option

  • Save icedraco/42eed096b1349e21fd25 to your computer and use it in GitHub Desktop.

Select an option

Save icedraco/42eed096b1349e21fd25 to your computer and use it in GitHub Desktop.
A piece of code that copies the first line from file A into file B. Used to copy CSV headerss
# We take CSV headers from this file...
src_file = "source.csv"
# ...and add to this file as the first line
dst_file = "destination.csv"
# Output file that's created (to avoid possibly damaging the destination file)
output_file = "output.csv"
# Read the first line from source and add to output
headers = open(src_file, 'r').readline()
fd = open(output_file, 'w')
fd.write(headers)
# Copy destination file into output line-by-line (to avoid reading large files into RAM)
for line in open(dst_file, 'r'):
fd.write(line)
fd.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment