1

I have a text file mycontacts.txt

enter image description here

I need to read content of this file. Location path is: C:\Users\myusername\Documents\SQL_NeedToKnow\Python I am using example from here: https://medium.freecodecamp.org/send-emails-using-code-4fcea9df63f

# 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

So for my case, seems like I need to change the directory first. For that I am using os.chdir method. Not sure where should I place it exactly, but I dont even have an error. I am using Jupyter. I am trying to do this:

# Function to read the contacts from a given contact file and return a
# list of names and email addresses
import os
os.chdir(r'''C:\Users\oserdyuk\Documents\SQL_NeedToKnow\Python''')
def get_contacts(filename):
    names = []
    emails = []
    with open("mycontacts.txt", 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

I also tried usin Open with full path:

# Function to read the contacts from a given contact file and return a
# list of names and email addresses
import os
#os.chdir(r'''C:\Users\oserdyuk\Documents\SQL_NeedToKnow\Python''')
def get_contacts(filename):
    names = []
    emails = []
    with open(r'''C:\Users\oserdyuk\Documents\SQL_NeedToKnow\Python''', 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
1
  • open with full path Commented Dec 5, 2017 at 7:25

2 Answers 2

2

With your code I am assuming you're trying to get name and email in two lists from the entries in given file.

So with some edits in your code, try this:

def get_contacts(filename):
    names = []
    emails = []
    with open(filename, 'r') as contacts_file:
        for row in contacts_file.readlines():  # this is what missing
            name, email = row.split(",")  # split will be with "," delimiter
            names.append(name)
            emails.append(email)
    return names, emails
Sign up to request clarification or add additional context in comments.

5 Comments

So I should pass the path of the file in Open, correct? with open(r'''C:\Users\MyUserName\Documents\SQL_NeedToKnow\Python''') as contacts_file:
you can pass the entire path location of the file, not just the directory, the whole path in your case will be ..\Python\mycontacts.txt. Or, you can have the file within the same dir as the script and simply pass the filename "mycontacts.txt"
I did exactly how you recommended, but it says "No such file or directory". with open(r'C:\Users\MyUserName\Documents\SQL_NeedToKnow\Python\mycontacts.txt') as contacts_file:
I got it. The file name should be like that: mycontacts.txt.txt
Did it work? I am not sure if .txt.txt will work. I have worked on *nix systems, not much on windows though, but I think the problem could be in the writing the path, where '\' should be '\\' in (e.g. "C:\\Users\\Desktop\\file.txt" ), because of special characters thing. Also, look this link. Here you can find detailed explanation for working with files and directories, they also have examples for both windows and *nix systems.
0

In the function, you are trying to open the actual file and you are not using the function parameter.

Rewriting the function:

def get_contacts(filename):    
    names = []
    emails = []

    with open(filename) as contacts_file:
        for a_contact in contacts_file.readlines():
            name, email = a_contact.strip().split(', ')
            names.append(name)
            emails.append(email)

    return names, emails

# When you call the function, pass the file path as an argument
print(get_contacts('test.txt'))

(['Abc', 'Xyz'], ['[email protected]', '[email protected]'])
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.