1

I've created a text file that just has an unformatted list of emails, all on a new line. Ex:

This is the unformatted list called emailListTest.txt

a@a.org
b@b.org
c@c.org
d@d.org
e@e.org
f@f.org
g@g.org
h@h.org

I have another app that opens and reads the unformatted email list file. Then it creates a new python file and tries to put the unformatted list of emails all in a python list.

This all started because I wanted to learn how to send emails with code. I've made another app that does this, but I wanted one that sends emails to multiple people at the same time.

This is my writer app

emailListFile = open("emailListTest.txt", "r")
emailListFileList = open("emailListTestList.py","w")


print(emailListFileList.write("emailAsList = "))
print(emailListFileList.write("["))

def emailAddressList():
    #try:
    for i in emailListFile:
        print(emailListFileList.write( "\n'" + i + "'" + ", "))


emailAddressList()

print(emailListFileList.write("]"))



emailListFile.close()
emailListFileList.close()

I must be missing something because the output is

emailAsList = [
'a@a.org
', 
'b@b.org
', 
'c@c.org
', 
'd@d.org
', 
'e@e.org
', 
'f@f.org
', 
'g@g.org
', 
'h@h.org', ]

I feel like it's so close to being right, if only there weren't these new lines from the unformatted list file splitting the ', onto a new line.

If anyone knows how to solve my issue, I'd greatly appreciate it! (Also, if anyone has any other tips to simplify other parts in my code, I'd love to know. I'm still learning!)

Thank you!

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Atmosquake
  • 11
  • 1
  • This is an unnecessarily convoluted approach. You can read the contents of a file and split all lines into a list in your other script. You don't need to write an intermediate .py file – Pranav Hosangadi Feb 27 '23 at 19:10
  • Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – Pranav Hosangadi Feb 27 '23 at 19:14

2 Answers2

0

Why you need to do it at the first place? You can just open .txt file inside emailListTestList.py and load it lines to list:

emaillist_file = open("email_list.txt", "r")
emails = [l.strip() for l in emaillist_file.readlines()]

print(emails)

# Outputs: ['a@a.org', 'b@b.org', 'c@c.org', 'd@d.org', 'e@e.org', 'f@f.org', 'g@g.org', 'h@h.org']

If you still want to 'transfer' one python-object from one script to another - you should use python library pickle. This library writes python object into binary file, and also can read those objects from such binary files

Usage:

email_list.txt
b@b.org
c@c.org
d@d.org
e@e.org
f@f.org
g@g.org
h@h.org
script_1.py
import pickle

email_list_file = open("email_list.txt", "r")
email_list = [line.strip() for line in email_list_file.readlines()]

print(email_list)
# Outputs ['a@a.org', 'b@b.org', 'c@c.org', 'd@d.org', 'e@e.org', 'f@f.org', 'g@g.org', 'h@h.org']

print(type(email_list))
# Outputs <class 'list'>

with open("email_list_obj.bin", "wb") as binfile:
    # Write email_list object into email_list_obj.bin file
    pickle.dump(email_list, binfile)
script_2.py
import pickle

with open("email_list_obj.bin", "rb") as binfile:
    email_list = pickle.load(binfile)

print(email_list)
# Outputs ['a@a.org', 'b@b.org', 'c@c.org', 'd@d.org', 'e@e.org', 'f@f.org', 'g@g.org', 'h@h.org']

print(type(email_list))
# Outputs <class 'list'>
purity
  • 108
  • 5
0

As others have pointed out, this is not the approach you want to take unless you have a very good reason (see later). However, as you asked the question, and in the hope it helps you with file I/O ideas for future programs, this is one way to achieve the result you want:

with open("emailListTest.txt") as f_in:
    with open("emailListTestList.py", "w") as f_out:
        f_out.write("email_list = [\n")
        for line in f_in:
            email = line.strip()  # Removes whitespace (e.g. \n)
            f_out.write(f"    {email}, \n")
        f_out.write(']')

(The f_out.write(f" {email}, \n") statement utilises "f-strings".)

However, you are best served by having an external data source (a simple text file is fine in this scenario) which you already have. Your Python program should read that text file in and create the list dynamically each time your program runs. That way you can add and remove entries from the text file (your data source for emails) without constantly having to change "hard coded" entries from your program itself (from your list assignment).

To do this, you can write:

with open("emailListTest.txt") as f_in:
    email_addresses = [line.strip() for line in f_in]
print(email_addresses)

If you haven't come across statements like [line.strip() for line in f_in] before, they are known as list comprehensions and are a very powerful and useful feature of Python.

bigkeefer
  • 576
  • 1
  • 6
  • 13
  • Awesome! Thank you so much! I will definitely take a look at the list comprehensions link that you provided here! I didn't know about the line.strip() thing too. – Atmosquake Feb 27 '23 at 21:19