I have a text file mycontacts.txt
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

openwith full path