Search
Close this search box.

Making a simple command line to do list in Python

I’ve been asked recently what some super simple tasks would be to support learning Python so I’ve decided to post a few of them. Yesterday, you may have seen my article, looking at how we might create a command line quiz, with a time limit. Today, I am going to look at how we might create a to do list.

Our to do items will be stored in a CSV. So this task will require us to read the CSV, take user input and write the changes back to that CSV.

Within the main function, we ask the user for a number of inputs (what do you want to do: show open tasks; show closed tasks or update a task). Depending on which they choose, we then call a function – with the exception of update, where we confirm what exactly it is they want to update.

I’ve added a couple of while loops here. You don’t just want the application to close when the user completes a task, like ‘show open tasks’. That would be rubbish, so we want to continue letting them choose what they want to do until they tell us we are done. The other loop forces the user to enter commands that are accepted by the app – if the command isn”t in the list, they’re asked to enter a valid one.

If they choose to update, they are asked which task ID they would like to update, which field they want to update and what the new value is.

#!/usr/bin/env python
import csv
tasks = 'tasks.csv'
action = ''
done = 'NO'
while done == 'NO':
    while action not in ('SHOW OPEN', 'SHOW CLOSED', "UPDATE"):
        action = input('You must choose: SHOWOPEN, SHOWCLOSED or UPDATE')
    if action == 'SHOWOPEN':
        showOpen()
        action = input('Are you done (YES or NO)')
    elif action == 'SHOWCLOSED':
        showClosed()
        action = input('Are you done (YES or NO)')
    elif action == 'UPDATE':
        showOpen()
        id_update = input('Which Task ID to Update')
        id_update = str(1)
        update_field = input('DESCRIPTION, DATE or STATUS update?')
        update_value = input('New Value')
        updateTask(id_update, update_field, update_value)
        action = input('Are you done (YES or NO)')

So now, let’s look at the showOpen function. Here, we read in the CSV file and print all of the rows that don’t have the word ‘Complete’ in them.

def showOpen():
    with open(tasks) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            if 'complete' not in row:
                print(row)

Show closed, is the opposite – only show rows of the CSV that include the word ‘Complete’.

def showClosed():
    with open(tasks) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            if 'complete' in row:
                print(row)    

Finally, we are going to update the task and write it back to the CSV. Here, we again open the CSV and find the record where the task ID matches the one that the user wants to update. We then, update the value of that field to be the update value that the user entered. We finally, append the row to a list called x. So every row in the CSV should now reside in the list x.

We then call the writeback function, which overwrites the original tasks.csv.

def updateTask(id, field, update_value):
    with open(tasks) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        
        x = []
        for row in csv_reader:
            if row[0] == id:
                if field == 'DESCRIPTION':
                    row[1] = update_value
                elif field == 'DATE':
                    row[2] = update_value
                elif field == 'STATUS':
                    row[3] = update_value
            x.append(row)
            writeback(x)
            
def writeback(x):
    with open('tasks.csv', 'w') as writeFile:
        writer = csv.writer(writeFile)
        writer.writerows(x)

The full code:

#!/usr/bin/env python
import csv
tasks = 'tasks.csv'
action = ''
done = 'NO'
while done == 'NO':
    while action not in ('SHOW OPEN', 'SHOW CLOSED', "UPDATE"):
        action = input('You must choose either: ADD or READ')
    if action == 'SHOW OPEN':
        showOpen()
        action = input('Are you done (YES or NO)')
    elif action == 'SHOW CLOSED':
        showClosed()
        action = input('Are you done (YES or NO)')
    elif action == 'UPDATE':
        showOpen()
        id_update = input('Which Task ID to Update')
        id_update = str(1)
        update_field = input('DESCRIPTION, DATE or STATUS update?')
        update_value = input('New Value')
        updateTask(id_update, update_field, update_value)
        action = input('Are you done (YES or NO)')
		
def showOpen():
    with open(tasks) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            if 'complete' not in row:
                print(row)
            
def showClosed():
    with open(tasks) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            if 'complete' in row:
                print(row)    
            
def updateTask(id, field, update_value):
    with open(tasks) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        
        x = []
        for row in csv_reader:
            if row[0] == id:
                if field == 'DESCRIPTION':
                    row[1] = update_value
                elif field == 'DATE':
                    row[2] = update_value
                elif field == 'STATUS':
                    row[3] = update_value
            x.append(row)
            writeback(x)
            
def writeback(x):
    with open('tasks.csv', 'w') as writeFile:
        writer = csv.writer(writeFile)
        writer.writerows(x)
Share the Post:

Related Posts