Unsupported operation :not writeable python

Question:

Email validation

#Email validator
import re


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)

I am wondering why my data wont write to the disk. Python says that my operation is not supported.

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

||

Answers:

You open the variable “file” as a read only then attempt to write to it:

file = open('ValidEmails.txt','r')

Instead, use the ‘w’ flag.

file = open('ValidEmails.txt','w')
...
file.write(email)
Answered By: triphook
file = open('ValidEmails.txt','wb')
file.write(email.encode('utf-8', 'ignore'))

This is solve your encode error also.

Answered By: Anurag Misra

use this :

#Email validator
import re


def is_email():
    email=input("Enter your email")
    pattern = '[.w]{1,}[@]w+[.]w+'
    file = open('ValidEmails.txt','w')
    if re.match(pattern, email):
        file.write(email)
Answered By: Avijit Chowdhury
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.