from __future__ import unicode_literals, print_function
import os,csv
import sys

def overwrite_previous_file(previous_file,new_file):
    with open(previous_file, 'w') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=str('\t'), quotechar=str('|'), quoting=csv.QUOTE_MINIMAL)

        with open(new_file) as csvfile1:
            spamreader = csv.reader(csvfile1, delimiter=str('\t'), quotechar=str('|'), quoting=csv.QUOTE_MINIMAL)

            for row in spamreader:
                row=[rii.encode('utf-8') for rii in row ]
                spamwriter.writerow(row)

def append_to_previous_file(previous_file,new_file):
    with open(previous_file, 'a') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=str('\t'), quotechar=str('|'), quoting=csv.QUOTE_MINIMAL)

        with open(new_file) as csvfile1:
            spamreader = csv.reader(csvfile1, delimiter=str('\t'), quotechar=str('|'), quoting=csv.QUOTE_MINIMAL)

            for row in spamreader:
                col_title=row

                break

            for row in spamreader:
                row=[rii.encode('utf-8') for rii in row ]
                spamwriter.writerow(row)