Need some help with this Contact Manager Program

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1040 Views - Last Post: 20 March 2019 - 04:46 PM Rate Topic: -----

#1 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 23-September 11

Need some help with this Contact Manager Program

Posted 01 March 2019 - 05:34 PM

The error message displays: Traceback (most recent call last):
File "D:\Manager_2.py", line 101, in <module>
main()
File "D:\Manager_2.py", line 83, in main
contacts = read_contacts()
File "D:\Manager_2.py", line 19, in read_contacts
with open(FILENAME, newline="") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'contacts.csv'. I am new too Python.

##!/usr/bin/evn python3
#
# Contact Manager Program
#
import csv,os;



FILENAME = "contacts.csv"


def write_contacts(contacts):
    with open (FILENAME, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerows(contacts)

def read_contacts():
    contacts = []
    with open(FILENAME, newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            contacts.append(row)
            return contacts
##definition list command
def list_contacts(contacts):
    for i in range(len(contacts)):
        contact = contacts[i]
        print(str(i+1) + "." + contact[0])
        print()
##definition view command
def view_contacts(contacts):
    number = int(input("Number: "))
    if number < 1 or number > len (contacts):
        print("invalid number")
        
    else:

                 
            contact = contacts[number-1]
            print("Name: " + contact[0])
            print("Email: " + contact[1])
            print("Phone: " + contact[2])
            print()

##definition add command
def add_contacts(contacts):
    name = input("Name: ")
    email = input("Email: ")
    phone = input("Phone: ")
    contact = []
    contact.append(name)
    contact.append(email)
    contact.append(phone)
    contacts.append(contact)
    write_contacts(contacts)
    print(name + " was added")
    print()
    
##definition delete command
def delete_contacts(contacts):
    number = int(input("Number: "))
    if number < 0 or number > len (contacts):
        print("invalid number")
    else:
            contact = contacts.pop(number-1)
            write_contacts(contacts)
            print(contact[0] + " was deleted")
            return contacts
        
def display_menu():
    print("Contact Manager")
    print()
    print("COMMAND MENU")
    print("list - Display all contacts", "\nview - View a contact",
          "\nadd - Add a contact", "\ndel - Delete a contact",
          "\nexit - Exit program")
         
    print()
    
##definition main menu
def main():
    display_menu()
    contacts =  read_contacts()
    while True:
        command = input("Command: ")
        if command.lower() == "list":
            list_contacts(contacts)
        elif command.lower() == "view":
            view_contacts(contacts)
        elif command.lower() == "add":
            add_contacts(contacts)
        elif command.lower() == "del":
            delete_contacts(contacts)
        elif command.lower() == "exit":
            break
        else:
            print("Invalid command. Please try again.\n")
            print("Good bye!")
                            
if __name__ == "__main__":
    main()



Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with this Contact Manager Program

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14994
  • View blog
  • Posts: 59,862
  • Joined: 12-June 08

Re: Need some help with this Contact Manager Program

Posted 01 March 2019 - 05:35 PM

Is that file in the same spot as your python script?
Was This Post Helpful? 1
  • +
  • -

#3 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 23-September 11

Re: Need some help with this Contact Manager Program

Posted 03 March 2019 - 10:16 AM

I am not sure. I am new to python programming, and a lot of the concepts are new.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14994
  • View blog
  • Posts: 59,862
  • Joined: 12-June 08

Re: Need some help with this Contact Manager Program

Posted 03 March 2019 - 10:19 AM

Then try it by putting the file in the same folder as that .py script.
Was This Post Helpful? 0
  • +
  • -

#5 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Need some help with this Contact Manager Program

Posted 03 March 2019 - 10:40 AM

View Postgneisler, on 03 March 2019 - 10:16 AM, said:

I am not sure. I am new to python programming, and a lot of the concepts are new.


This doesn't just go for python programming... it's just about every language that I'm aware of. Check this out,

line 9:

FILENAME = "contacts.csv"

if you don't put something like C:\myDir\contacts.csv ... your program is going to assume that file is in the directory that you're running your program from. Like modi123_1 said, either move the file into the same directory as your program is running from OR explicitly assign the full path of that CSV file to your FILENAME
Was This Post Helpful? 0
  • +
  • -

#6 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 23-September 11

Re: Need some help with this Contact Manager Program

Posted 05 March 2019 - 10:20 AM

The program is finally working, thank everyone for the assistances.
Was This Post Helpful? 0
  • +
  • -

#7 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 23-September 11

Re: Need some help with this Contact Manager Program

Posted 07 March 2019 - 10:42 AM

I was able to locate the file but then when I type command list, to list all of the contact names, it only displays one name and not the other contacts names.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 04:18 AM

Post your up-to date code and describe whereabouts within it you think the problem is.
Was This Post Helpful? 0
  • +
  • -

#9 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 23-September 11

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 10:54 AM

The problem could be the def list_contacts(contacts)function. Because only one name is being listed, instead of all of the contact names.



##!/usr/bin/evn python3
#
# Contact Manager Program
#
import csv



FILENAME = "contacts.csv"


def write_contacts(contacts):
    with open (FILENAME, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerows(contacts)

def read_contacts():
    contacts = []
    with open(FILENAME, newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            contacts.append(row)
            return contacts

def list_contacts(contacts):
    for i in range(len(contacts)):
        contact = contacts[i]
        print(str(i+1) + "." + contact[0] )
        print()

def view_contacts(contacts):
    number = int(input("Number: "))
    if number < 1 or number > len (contacts):
        print("invalid number")
        
    else:

                 
            contact = contacts[number-1]
            print("Name: " + contact[0])
            print("Email: " + contact[1])
            print("Phone: " + contact[2])
            print()


def add_contacts(contacts):
    name = input("Name: ")
    email = input("Email: ")
    phone = input("Phone: ")
    contact = []
    contact.append(name)
    contact.append(email)
    contact.append(phone)
    contacts.append(contact)
    write_contacts(contacts)
    print(name + " was added")
    print()
    

def delete_contacts(contacts):
    number = int(input("Number: "))
    if number < 0 or number > len (contacts):
        print("invalid number")
    else:
            contact = contacts.pop(number-1)
            write_contacts(contacts)
            print(contact[0] + " was deleted")
            return contacts
        
def display_menu():
    print("Contact Manager")
    print()
    print("COMMAND MENU")
    print("list - Display all contacts", "\nview - View a contact",
          "\nadd - Add a contact", "\ndel - Delete a contact",
          "\nexit - Exit program")
         
    print()
    

def main():
    display_menu()
    contacts =  read_contacts()
    while True:
        command = input("Command: ")
        if command.lower() == "list":
            list_contacts(contacts)
        elif command.lower() == "view":
            view_contacts(contacts)
        elif command.lower() == "add":
            add_contacts(contacts)
        elif command.lower() == "del":
            delete_contacts(contacts)
        elif command.lower() == "exit":
            print("Good bye!")
            break
        else:
            print("Invalid command. Please try again.\n")
                           
if __name__ == "__main__":
    main()


Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 11:16 AM

Why do you think that is? The function is pretty small, so it shouldn't be hard to see the problem.
Was This Post Helpful? 0
  • +
  • -

#11 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 12:01 PM

Can you upload a redacted copy of your CSV file? I don't know python but I'm messing around with it now in VS. I'm using a printer list I exported from powershell and getting other exceptions

EDIT NVM I'm just going to use a text file. I don't need to get into the other parts of your program. And yes, this is confusing... code looks good to me

This post has been edited by hexagod: 08 March 2019 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 12:27 PM

The problem isn't your list function it's your def read_contacts(): ... only reading one line from the CSV. This is my first time ever using python and I can't say I'm much of a fan (after messing around with it)
Was This Post Helpful? 0
  • +
  • -

#13 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 12:41 PM

So far I am very confused by how python works.. I'm headed off to lunch ATM but I think that your read_contacts() function doesn't read all the lines for some reason.

I tried printing directly out of that function when the app loads to get a feel for what was returning and it only prints one line.

This will print all lines but I can't get the append working.

def read_contacts():
    contacts = []
with open('contacts.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        print(row)
        print(row[0])
        print(row[0],row[1],row[2],)


not sure if it helps or not but I'm out, brb
Was This Post Helpful? 0
  • +
  • -

#14 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2836
  • View blog
  • Posts: 11,132
  • Joined: 03-December 12

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 01:15 PM

As @hex pointed out, your issue is here:
def read_contacts():
    contacts = []
    with open(FILENAME, newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            contacts.append(row)
            return contacts



Look where the return statement is. What do you think that is actually doing?
Was This Post Helpful? 0
  • +
  • -

#15 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Need some help with this Contact Manager Program

Posted 08 March 2019 - 01:30 PM

yep, all about the indentation... python is strange. Now I know too. Loop was returning a new contacts for each row

darnit he beat me to it... should have skipped lunch =] :bananaman:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2