python文件操作

步骤:打开文件->操作文件(读、写等)->关闭文件

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():返回值为整个文件内容的列表,每项是以换行符为结尾的一行字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值