In this tutorial we will be going over how to make a notes template program. This program is a simple one run on the command line that create us a file to store our notes and todos for the day. The file output will look like this:

## notes_YYYYMMDD.md

### NOTES

### TODO

### INPOGRESS

### TODONE

where the file name is also notes_YYYYMMDD.md . I find that this is a simple program to create that generally get you a bit more comfortable with a language and actually creates a useful tool for you. And too top it off it gets you to start using the internal time library for a language and get used to file operations as well.

Outline

It's best to start out a program with an outline so that you know what needs to be done and so that you can be on track. Our program will need to do the following:

  • Get the current date
  • Create the template to write to the file
  • Write the given template to the file

We'll be writing this program in a single main.py file and we should be able to just run it on the command line. So let's get started and if you really want the full code sample it is at the bottom of the page.

Getting the Current Date


To get the current date we will use the python datetime library. For our get_note_date function we will take in nothing and return a string that is comprised of the year month and day in the following form: YYYYMMDD. One portion of this that is an edge case is how to ensure that the month and the day are always two digits. To solve that we check if the len of the date or month string are less than 2 characters and prepend a "0" to them.

from datetime import date

def main():
  print("=== Creating notes ===\n")
  noteDate = get_note_date()
  print(noteDate)

def get_note_date() -> str:
  today = date.today()
  year = str(today.year)
  month = str(today.month)
  if len(month) < 2:
    month = "0" + str(month)

  day = str(today.day)
  if len(day) < 2:
    day = "0" + day

  noteDate = year + month + day
  return noteDate

if __name__ == '__main__':
  main()

Once you have that put together give it a run by entering the directory that your main.py file is located in and running either: python main.py or python3 main.py

Creating the Template

In this portion we will create the template that will be our notes. We will need a function that takes in the date string and returns us a string that we will then write to the file. It takes the following form:

from datetime import date

def main():
  print("=== Creating notes ===\n")
  noteDate = get_note_date()

  template = get_notes_template(noteDate)

  print(template)

def get_note_date() -> str:
  today = date.today()
  year = str(today.year)
  month = str(today.month)
  if len(month) < 2:
    month = "0" + str(month)

  day = str(today.day)
  if len(day) < 2:
    day = "0" + day

  noteDate = year + month + day
  return noteDate

def get_notes_template(date:str) -> str:
  template="## notes_{}.md\n\n### NOTES\n\n### TODO\n\n### INPOGRESS\n\n### TODONE\n\n".format(date)

  return template

if __name__ == '__main__':
  main()  

Writing to the Notes File

For this final portion we will need to create a function that will take in both a date and the template and write the notes template to a file. The date is needed for the filename and the template is needed as that is what will be the content inside of the file. You can find the completed code below.

from datetime import date

def main():
  print("=== Creating notes ===\n")
  noteDate = get_note_date()

  template = get_notes_template(noteDate)

  write_notes_file(noteDate, template)

  print("=== Notes have been created ===\n")

def write_notes_file(date:str, template:str):
  # write to the note file
  fileName = "notes_{}.md".format(date)
  f = open(fileName, "w")
  f.write(template)
  f.close()
  print("=== " + fileName + " has been written ===\n")

def get_note_date() -> str:
  today = date.today()
  year = str(today.year)
  month = str(today.month)
  if len(month) < 2:
    month = "0" + str(month)

  day = str(today.day)
  if len(day) < 2:
    day = "0" + day

  noteDate = year + month + day
  return noteDate

def get_notes_template(date:str) -> str:
  template="## notes_{}.md\n\n### NOTES\n\n### TODO\n\n### INPOGRESS\n\n### TODONE\n\n".format(date)

  return template

if __name__ == '__main__':
  main()

Following the code through this example we have now been exposed to some of the simple libraries in python and ones that you will likely encounter again. Happy coding!