[英]How to Write to a file and then read the file in Python
我写了以下代码:
supported = open("Mails/mymails.txt",'w')
with open("emails.txt","r") as handle:
for line in handle:
if line.strip():
list = line.split(":")
email = list[0]
domain = email.split("@")
if domain[1] in blacklist:
blacklisted = blacklisted+1
if domain[1] in dictionary and domain[1] not in blacklist:
valid=valid+1
supported.write(domain[1])
print(valid,blacklisted)
with open("Mails/mymails.txt",'r') as handle1:
for line1 in handle1:
print(line1)
我使用了 append 模式并且工作正常,但是当我使用多个文件时,我必须手动删除 support.txt 文件以确保新文件行不会附加到旧文件行任何帮助表示赞赏
我关闭了文件,它工作正常
喜欢: supported.close()
在第一个with
语句的末尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.