Unsupported operation :not writeable python

PythonPython 3.x

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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)


Solution 2 - Python

file = open('ValidEmails.txt','wb')
file.write(email.encode('utf-8', 'ignore'))

This is solve your encode error also.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLenard View Question on Stackoverflow
Solution 1 - PythontriphookView Answer on Stackoverflow
Solution 2 - PythonAnurag MisraView Answer on Stackoverflow