Q:

Python csv not writing to file

I am trying to write to a .tsv file using python's CSV module, this is my code so far
file_name = "test.tsv"
TEMPLATE = "template.tsv"
fil = open(file_name, "w")
# Added suggested change 
template = csv.DictReader(open(TEMPLATE, 'r'), delimiter='\t')
new_file = csv.DictWriter(fil, fieldnames=template.fieldnames, delimiter='\t')
new_file.writeheader()

basically TEMPLATE is a file that will contain the headers for the file, so i read the headers using DictReader and pass the fieldnames to DictWriter,  as far as i know the code is fine, the file test.tsv is being created but for some reason the headers are not being written. 
Any help as to why this is happening is appreciated, thanks.

A:

DictReader's first argument should be a file object (create with open()), cf. http://docs.python.org/py3k/library/csv.html#csv.DictReader
You forgot open() for the TEMPLATE file.
import csv

file_name = "test.tsv"
TEMPLATE = "template.tsv"
fil = open(file_name, "w")

# you forgot this line, which will open the file
template_file = open(TEMPLATE, 'r')

template = csv.DictReader(template_file, delimiter='\t')
new_file = csv.DictWriter(fil, fieldnames=template.fieldnames, delimiter='\t')
new_file.writeheader()

