Contact Storer in Python for Beginners with Full Code

Contact Storers or contact books may be out of fashion now, thanks to the intelligence of smartphones and laptops. However, they are still helpful when you need to remember many numbers. Using CSV (comma-separated values) files, we can use Python to build a simple contact storage system with a user-friendly text menu to store and recover data whenever needed. Here's how you can do that:

First, we start with the basic setup. Python cannot handle CSV files by itself, so we import the csv module. The csv module comes with a csv reader and writer, making CSV file management far simpler. 

Then we need to check whether a contactbook.csv has already been created. We must remember that when we open a file in write mode and a file doesn't already exist in that name, Python makes a new file in that name. However, if you try to open a file that doesn't exist in read mode, Python throws an error. Using this to our advantage, we can use a quick try-except method to check for an existing file. If we get an error, we can move to the except and create a new file with the write mode. Here, we also need to give the headings for the columns. We can do this using the writerow function, which adds one row to the CSV file.

Once the basic setups are done, we can start with the basic code. We need to give the user three options, a data entry mode, a data recovery mode, and an exit function. We start with an infinite loop, giving the user a chance to keep working until he wants to exit. Then, we get the input from the user on what he wants to do. There are many ways of doing this, but a simple way is to get a number and assign each number to an option. Once that's done, we need to work on the main logic.

We need to open the file in append mode for the entry system. This is an important fact to remember here. Opening the file in write mode will lead to overwriting previously entered data, which is not what we want. Once we open the file, we get the name and number from the user and store it as a list. We can then use the writerows method to append it to the main file. Don't forget to use the file.flush() function to save the data in the file. 

For the data recovery function, we need to open the file in read mode. Then, we can get all the data from the file using the reader function. This data would be in the form of a collection of lists. From here, we can just iterate through the collection to get all the rows of data. There are some empty lists in the collection, so we need to remove those in our checking parameter, or it might lead to errors. Once that's done, we need to get a name input from the user and use it as the parameter to get the corresponding number of the name using an if-statement. 

The exit function is nothing but a break statement to leave the loop. 

So, combining all these snippets, we get a code that looks like this:

#Setup

import csv

try:
    csvfile = open("contactbook.csv","r")

except:
    csvfile = open("contactbook.csv","w")
    heads = ["Name","Contact"]

    writer = csv.writer(csvfile)

    writer.writerow(heads)

#Menu

print("Welcome to the Contact Book\n\nType 1 for getting number\n\nType 2 for entering new data\n\nType 0 to exit\n")

#Main Code

while True:


    entry_option = int(input("\nEnter your option: "))

    if entry_option == 1:

        csvfile = open("contactbook.csv","r")
        
        name = input("\nEnter name: ")

        data = csv.reader(csvfile)

        try:

            for lines in data:
                if lines != [] and lines[0] == name:
                    print("\n" + lines[1] + " is the number of " + lines[0])
                    break
            else:
                print("\nName not found")

        except:

            print("\nNo Data")
            
                

    elif entry_option == 2:

        csvfile = open("contactbook.csv","a")
        
        name = input("\nEnter name: ")
        number = input("\nEnter number: ")

        entry = [[name,number]]

        writer = csv.writer(csvfile)

        writer.writerows(entry)

        print("\nData entered\n")

    elif entry_option == 0:

        break

    else:
        print("\nInvalid option\n")

    csvfile.flush()

    
    

You can always try to improve the program by adding more features, like a better UI and more columns. However, the basic code would still be similar to this. 

And that's that. Here's how you make your very own digital Contact Book with CSV files.

If you liked this post, feel free to share it with anyone you think will find it helpful. The comment section here is always open for queries and suggestions, though I would suggest not deviating from the topic. 

If you like this, you might want to check these out as well:

Text-File Handling

Python Blackjack Simulator (With Full Code)


Comments