Saving each result on a new line to a csv file, using python
Question: I've been trying to create a csv file using python and save a new date on a
new row. My intention is to replicate the shell output(shown below), in csv
file. However the output I get is only saving the last result and iterating
through one date(shown in the csv output below). I'm sure there's an error in
my approach but I'm stumped. thanks in advance.

    
    
    from datetime import timedelta, date
    import csv
    
    def daterange(start_date, end_date):
        for n in range(int ((end_date - start_date).days)):
            yield start_date + timedelta(n)
    
    start_date = date(2014, 12, 28)
    end_date = date(2015, 1, 10)
    
    for single_date in daterange(start_date, end_date):
        new_date = single_date.strftime("%Y-%m-%d")
        with open('output.csv', 'wb') as f:
            writer = csv.writer(f)
            writer.writerows(new_date)
    

[shell output](http://i.stack.imgur.com/qg6Le.png) [csv
output](http://i.stack.imgur.com/bHTHg.png)


Answer: When you open a file with `w` mode, python truncates the file. You should open
it only once:

    
    
    with open('output.csv', 'wb') as f:
        writer = csv.writer(f)
        for single_date in daterange(start_date, end_date):
            new_date = single_date.strftime("%Y-%m-%d")
            writer.writerow(new_date)
    

