不支持的操作:不可写的python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27278755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:36:51  来源:igfitidea点击:

Unsupported operation :not writeable python

pythonpython-3.x

提问by Lenard

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.

我想知道为什么我的数据不会写入磁盘。Python 说不支持我的操作。

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

采纳答案by triphook

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.

相反,使用“w”标志。

file = open('ValidEmails.txt','w')
...
file.write(email)

回答by Anurag Misra

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

This is solve your encode erroralso.

这也是解决你的encode error