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

时间:2014-12-03 18:13:32

标签: python python-3.x

电子邮件验证

#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")

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

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

我应该将电子邮件转换为此类字符串还是

file.write(str(email))

是别的什么

我可能错过了很简单的事情。

2 个答案:

答案 0 :(得分:46)

您将变量“file”作为只读打开然后尝试写入它。使用'w'标志。

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

答案 1 :(得分:-1)

{
account: {
  id: 1,
  company: "account1",
  created_at: "2017-01-11T12:48:00.107Z",
  updated_at: "2017-08-10T11:10:56.318Z",
  documents: [{
    id: 1,
    user_id: null,
    account_id: 1,
      document_details: [
        {
          id: 1,
          document_id: 1,
          title: "test11",
          description: "test1x"
        },
        {
          id: 2,
          document_id: 1,
          title: "test12",
          description: ""
        }
      ],
      payments: [ ]
    }],
  costs: [
  {
    id: 1,
    account_id: 1,
    amount: 2,
    supplier_id: 1,
    invoice: "12",
    tax: 1,
    created_at: "2017-08-10T11:11:14.582Z",
    updated_at: "2017-08-10T11:11:14.582Z"
  }
  ],
  suppliers: [
  {
    id: 1,
    account_id: 1,
    title: "Mme",
    created_at: "2017-08-10T11:03:55.334Z",
    updated_at: "2017-08-10T11:03:55.334Z"
  }
  ],
  setting: {
    id: 1,
    account_id: 1,
    created_at: "2017-08-10T12:31:22.400Z",
    updated_at: "2017-08-10T12:31:22.400Z"
  },
  logo: {
    id: 1,
    account_id: 1,
    logo_base64: "logobase64"
  }
}
}

这也解决了您的file = open('ValidEmails.txt','wb') file.write(email.encode('utf-8', 'ignore'))