Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created October 7, 2016 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8cadf1b238f5dfea2e29133cb3026670 to your computer and use it in GitHub Desktop.
Save anonymous/8cadf1b238f5dfea2e29133cb3026670 to your computer and use it in GitHub Desktop.
Python function to read a list of contacts from a given file.
# Function to read the contacts from a given contact file and return a
# list of names and email addresses
def get_contacts(filename):
names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
@deeproverb
Copy link

Traceback (most recent call last):
File "C:/Users/Lenovo/Desktop/Mailing.py", line 43, in
for name, email in zip(names, emails):
NameError: name 'names' is not defined

I am getting this error. Can some please help?

@LorianDo
Copy link

LorianDo commented Aug 7, 2019

I got an index error: list index out of range in line: names.append(a_contact.split()[0]). A half month ago, when I tried it first time, it was successfully executed, but after a period of time, I got an error above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment