步骤:打开文件->操作文件(读、写等)->关闭文件
1.打开文件:建立文件与程序的关联
open(filename,mode)
filename:文件名(包括路径);mode:打开模式
打开模式 | 含义 |
r | 只读,文件不存在则报错 |
w | 只写,文件不存在则自动创建 |
a | 在文件末尾附加 |
r+ | 读写 |
操作文件:写入,读取,等
写入操作:从计算机内存向文件写入数据
write():将文本数据写入文件中
writelines():将字符串列表写入文件中
3.关闭文件:终止程序与文件的关联
close()
# -*- coding:utf-8
def isNumberLoop(password):
for c in password:
if c.isnumeric():
return True
return False
def isPhal(password):
for c in password:
if c.isalpha():
return True
return False
def main():
password = input("请输入密码")
password_level = 0
password_len = len(password)
if password_len>8:
password_level += 1
else:
print("密码长度太短")
if isNumberLoop(password):
password_level += 1
else:
print("密码没有数字")
if isPhal(password):
password_level += 1
else:
print("密码没有字母")
if password_level !=3:
print("级别不够")
else:
print("密码不错哦")
f = open('password_3.0.txt','a')
f.write('密码:{},强度:{}\n'.format(password,password_level))
f.close()
if(__name__ == '__main__'):
main()
读取操作:从文件中读取数据到计算机内存中
read():返回值为包含整个文件内容的一个字符串
readline():返回值为文件下一行内容的字符串
readlines():返回值为整个文件内容的列表,每项是以换行符为结尾的一行字符串