Hey guys! This is my very first post on this website.

Anyways, I just started learning Python, and am reading an ebook, called "A Byte Of Python".
So at the end, it says to create an address book application.

In my application, it basically just creates a text file with all the stored info.

What you can do is: browse contacts, add contacts, search for contacts.

Now, I need help on: deleting contacts, modifying contact information.

Could anyone please just help me with what code I would need to use to define a class to delete a specific line from the text file, and also to modify a specific line in the text file? For the modification though, I would prefer if you could help me in creating a class which only modifies the part of the line which the user requests (only mod phone number, email, etc.)

By class I do mean code like:

def class_here():
    stuff here
    etc.

I have tried pretty much everything for modifying and deleting. For modifying, I have almost gotten the code, except that it does not actually change any information. It just implies that it worked. I had the code, but undid it in frustration and now I do not feel like writing it up again. It did not work anyways.

And as for deleting, I would like if the code could just delete a line specified by the user (e.g. enter name to delete: random name here -- delete the whole line of info).

I would appreciate if anyone could just give me the code and knowledge to start. It's not like I am asking to do the whole thing for me.

The code I have so far is:

# Filename: addressbook.py
# Author: Aman Dureja

temp1 = open("addressbookdata.txt", "a")

def collect_info():
    print("Okay, so this is where you input information about the contact!  Let's start!")

    lastname = input("What is the contact's last name? ")
    firstname = input("What is the contact's first name? ")
    address = input("Where does the contact live? ")
    phone = input("What is the contact's phone number? ")
    email = input("What is the contact's email address? ")

    print("Alright, so in a list, what we have is: ")
    contact = [lastname, firstname,\
               address,\
               phone,\
               email]
    print(contact)

    temp1 = open("addressbookdata.txt", "a")
    temp1.write(firstname + ' ' + lastname + ', ' + address + ', ' + phone + ', ' + email)
    temp1.write("\n")

def search_people():
    print("Alright, so here is where you would search for people already in the address book.")
    criteria = input("Please enter any keywords, including address, phone number, etc. for the search: ")

    temp1 = open("addressbookdata.txt", "r")
    for line in temp1:
        if criteria in line:
            print(line)
        else:
            print("Did not find person.  Please add him/her/it to the address book first.")

def browse_contacts():
    print("Alright, so this is where you will see a list of all your contacts, and all their information.")

    temp1 = open("addressbookdata.txt", "r")
    readfile = temp1.read()
    print (readfile)

So yeah, long post but sums stuff up. All I need is for someone to help with the delete and modify classes.

Thanks!

Next time push the (code) button before pasting code.

You must process file into list. Modify or delete. Write back with saving function. Usualy it is good to include option to quit without.

You are missing also the user menu. Now user must import the module and use functions from command line.

As for classes, I have feeling you are better off writing the user interaction to other file and use all these functions and modify/delete as imported. You would also need to change the existing functions to operate with list not the file directly. When everything works modular way, you can make other version object oriented way by adding classes for menu and processing information, basically by adding self parameters and init procedure. You can then change record type from list to list like object to go deeper to object orientedness.

Well, after some searching, I have finally got it. So this thread is pretty much pointless now :P

But I can post the finished code here if anyone wants it.

Well, after some searching, I have finally got it. So this thread is pretty much pointless now :P

But I can post the finished code here if anyone wants it.

Do it please, and mark the thread as solved.

You see, Daniweb is not for solving peoples problem. We share ideas. Sometimes people or new coders want solution but fails to learn how it is done.

Yes you got it working but do you undestand why it is working???? If you want to be a programmer, learn to understand why not how to.

mmmm ;)

^ Yes, I do understand everything. That is how I made it from scratch :P

and I will post my code once I finish the menu. It is not extremely user friendly right now, but I'll work on that minor stuff in v1.01 or something :)

Well, I finished the application!

If you see any bugs or stuff that could be improved, please tell me.

Also, I need help with one thing. When you do anything (add, modify, delete), the changes do not show up untill the user restarts the program. Could someone tell me how to change the code so that the changes show up immediately? If you do not know what I mean, basically when you add a new contact, and then try searching for it, it will not find it untill the user restarts the program.

Oh, and also, the application works in the IDLE, but not in terminal or the python launcher :S
(I am using Mac OS X 10.5 btw.)

But other than that, it is working great!
Code is here:

# Filename: addressbook.py
# Author: Aman Dureja

temp1 = open("addressbookdata.txt", "a")

def collect_info():
    print("Okay, so this is where you input information about the contact!  Let's start!")

    lastname = input("What is the contact's last name? ")
    firstname = input("What is the contact's first name? ")
    address = input("Where does the contact live? ")
    phone = input("What is the contact's phone number? ")
    email = input("What is the contact's email address? ")

    print("Alright, so in a list, what we have is: ")
    contact = [lastname, firstname,\
               address,\
               phone,\
               email]
    print(contact)

    temp1 = open("addressbookdata.txt", "a")
    temp1.write(firstname + ' ' + lastname + ', ' + address + ', ' + phone + ', ' + email)
    temp1.write("\n")

    print("The contact has been added.")
    restart()

def search_people():
    print("Alright, so here is where you would search for people already in the address book.")
    criteria = input("Please enter any keywords, including address, phone number, etc. for the search: ")

    temp1 = open("addressbookdata.txt", "r")
    for line in temp1:
        if criteria in line:
            print(line)
        else:
            print("Did not find person.  Please add him/her/it to the address book first.")

    restart()
    
def browse_contacts():
    print("Alright, so this is where you will see a list of all your contacts, and all their information.")

    temp1 = open("addressbookdata.txt", "r")
    readfile = temp1.read()
    print (readfile)

    restart()

def del_contact():
    import fileinput

    print("Here is where you can delete any contact you wish.")
    ass = input("Please enter any keywords of the contact (address, name, email, etc.): ")
    print("Okay, so all contacts wih", ass, "in their information are now being deleted from the address book.  Too late to go back now.")

    for line in fileinput.input("addressbookdata.txt", inplace =1):
        line = line.strip()
        if not ass in line:
            print (line)

    print("The contact has been deleted from the address book.")
    restart()

def mod_contact():
    print("Here is where you will be able to modify any contact's current information.")
    cname = input("What is the contact's name? ")

    temp1 = open("addressbookdata.txt", "r")
    if cname in temp1:
        print (line)
    elif cname not in temp1:
        print("Could not find contact.")

    tomod = input("What information would you like to modify?  Please enter the whole value (e.g. 'bob', 'random address', etc.) : ")

    temp1 = open("addressbookdata.txt", "r")
    if tomod in temp1:
        print(line)
    elif tomod not in temp1:
        print("Could not find specified information.")

    newinfo = input("Okay, please enter the new value for whatever you have decided to modify: ")

    print("Information is being updated.")

    temp1 = open("addressbookdata.txt", "a")
    for line in open("addressbookdata.txt"):
        line = line.replace(tomod, newinfo)
        temp1.write(line + "\n")
        print("Successfully modified data!")

    restart()

def menu():
    print("Welcome to addressbook version 1.0 from Aman Dureja!  This is my first application, so go easy on me. :)")
    print("In this adddress book, you will be able to add contacts, delete, modify info, search for contacts, and view your contacts in a list.")
    print("All your information gets saved.  *IN ORDER FOR ANY CHANGES TO REFLECT, PLEASE RESTART PROGRAM!!!*")
    choice = input("Okay, so time to make a choice: add contact [enter '1'], delete contact [enter '2'], modify current info [enter '3'], search contacts [enter '4'], browse contacts [enter '5']")
    if choice == '1':
        print("You have chosen to add a contact.")
        collect_info()
    elif choice == '2':
        print("You have chosen to delete a contact.")
        del_contact()
    elif choice == '3':
        print("You have chosen to modify an existing contact's info.")
        mod_contact()
    elif choice == '4':
        print("You have chosen to search for a specific contact.")
        search_people()
    elif choice == '5':
        print("You have chosen to browse your current list and see all your contacts with their info.")
        browse_contacts()
    else:
        print("Not a valid command.  Program will be restarted.")
        menu()

def restart():
    userinput = input("Would you like to restart the program to perform another operation? [y/n] ")
    if userinput == 'y':
        menu()
    elif userinput == 'n':
        print("Thank you for using addressbook by Aman Dureja!")
    else:
        print("Not a valid command.")
        restart()

menu()

Also, I need help with one thing. When you do anything (add, modify, delete), the changes do not show up untill the user restarts the program. Could someone tell me how to change the code so that the changes show up immediately? If you do not know what I mean, basically when you add a new contact, and then try searching for it, it will not find it untill the user restarts the program.

You need to commit the changes to your file. Writting to file does not write to file instanly until the file is closed. You must close the open files after reading or writting to them. Like that the info added will show straight away.

;)

Thanks. I fixed the code. I also made it a little bit more user friendly. Now my next project will be a text-based RPG!! :D

I am proud of you my goodfriend.

glad you made it.

Mark thread as solved and any upvoting is welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.