Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python file.write(email + '\n') NameError: name 'email' is not defined

i get this error from my code i dont get it why it isnt working

with open("emails.txt",'r') as file:
    for line in file:
        grade_data = line.strip().split(':')
        email = grade_data[0]
        password = grade_data[1]

with open("emails_sorted.txt",'a') as file:
    print(Fore.YELLOW + "Sorting email...")
    file.write(email + '\n')

with open("passwords.txt",'a') as file:
    print(Fore.YELLOW + "Sorting password...")
    passwordspecial = password + '!'
    file.write(passwordspecial + '\n')

print(Fore.GREEN + "Done!")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’re opening emails_sorted.txt after you’ve iterated through the contents of the other text files. You can fix this by either saving the data read from emails.txt and iterating through it again when you open emails_sorted.txt and passwords.txt:

emails = []
passwords = []

with open("emails.txt", "r", encoding="utf-8") as file:
    for line in file.readlines():
        grade_data = line.strip().split(":")
        emails.append(grade_data[0])
        passwords.append(grade_data[1])

with open("emails_sorted.txt", "a", encoding="utf-8") as file:
    print(Fore.YELLOW + "Sorting email...")
    for email in emails:
        file.write(email + "\n")

with open("passwords.txt", "a", encoding="utf-8") as file:
    print(Fore.YELLOW + "Sorting password...")
    for password in passwords:
        passwordspecial = password + "!"
        file.write(passwordspecial + "\n")

print(Fore.GREEN + "Done!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading