#!/usr/bin/env python3
import sys
from datetime import date, timedelta
def gen_calendar(year=None):
if year is None:
year = date.today().year
current = date(year, 1, 1)
delta = timedelta(days=1)
while current.year == year:
yield current.strftime('%a %b %d')
current += delta
if __name__ == '__main__':
year = None
if len(sys.argv) > 1:
year = int(sys.argv[1])
for str_date in gen_calendar(year):
print(str_date)
this will print to stdout so you can redirect it to a file from the console, which is the usual but if you want to write to the file directly from the script replace the last two lines with this:
with open('calendar.txt', 'w') as f:
for str_date in gen_calendar(year):
print(str_date, file=f)