Unsupported operation: non-writable python

Email Authentication

#Email validator import re f= open ('ValidEmails.txt', 'w') def is_email(): email=input("Enter your email") pattern = '[\.\w]{1,}[@]\w+[.]\w+' file = open('ValidEmails.txt','r') if re.match(pattern, email): file.write(email) file.close print("Valid Email") else: print("Invalid Email") #The Menu print("The Email validator progam \n") print("What do you want to do\n") print("Validate the Email") print("Quit") while True: answer=(input("Press V, or Q : ")) if answer in("V" ,"v"): is_email() elif answer in("Q" ,"q"): break else: print("Invalid response") 

I am wondering why my data is not being written to disk. Python says my operation is not supported.

 is_email file.write(email) io.UnsupportedOperation: not writable 

Should I convert the email to a string like this, or

 file.write(str(email)) 

is it something else

I probably missed something very simple.

+7
python
source share
2 answers

You open the file variable as read, and then try to write it. Use the 'w' flag.

 file = open('ValidEmails.txt','w') ... file.write(email) 
+21
source share
 file = open('ValidEmails.txt','wb') file.write(email.encode('utf-8', 'ignore')) 

It also resolves your encode error .

0
source share