Python file.write(email + 'n') NameError: name 'email' is not defined
Question:
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!")
Answers:
In the shown code, for line in file
, if file is empty, loop won’t run, thus email won’t be defined. Try to stop the process if file is empty, or set a default value for email
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!")
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!")
In the shown code, for line in file
, if file is empty, loop won’t run, thus email won’t be defined. Try to stop the process if file is empty, or set a default value for email
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!")