码迷,mamicode.com
首页 > 其他好文 > 详细

登陆和注册以及文件替换

时间:2019-09-18 19:43:29      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:dir   NPU   reg   pass   test   input   encoding   bre   flush   

# 注册
def register():
    count = 0
    while count < 3:
        Username = input("输入用户名:")
        password = input("输入密码:")
        re_password = input("再次输入密码:")
        if not re_password == password:
            print('两次输入不一致')
            count+=1
            continue
        with open('userinfo.txt', 'a', encoding='utf8')as fa:
            fa.write(f'{Username}:{password}\n')
            fa.flush()
            print('注册成功')
            break
# register()

# 登陆
def login():
    Username_inp = input("输入用户名:")
    password_inp = input("输入密码:")
    with open('userinfo.txt','r',encoding='utf-8')as fr:
       for userinfo in fr:
           username,password=userinfo.split(':')
           if username.strip()==Username_inp and password.strip()==password_inp:
               print('登陆成功')
               break
       else:
         print('登陆失败')

register()
login()

这里用了明天才会学的函数

# 缓存文件的原理
# 
# 同时打开多个文件
with open('test.py', 'r', encoding='utf8') as fr,         open('test_swap.py', 'w', encoding='utf8') as fw:
    data = fr.read()
    data = data.replace('sb', '傻逼')

    fw.write(data)

import os

os.remove('test.py')
os.rename('test_swap.py', 'test.py')


with open('test.py', 'r', encoding='utf8') as fr,         open('test_swap.py', 'w', encoding='utf8') as fw:
    # 再大的文件都能修改
    for i in fr:
        s = '傻逼'
        i = i.replace('sb', s)
        fw.write(i)
        # fw.flush()  # 先保存成功再继续运行


#import os

os.remove('test.py')
os.rename('test_swap.py', 'test.py')

#import os

print(os.listdir(r'位置'))

登陆和注册以及文件替换

标签:dir   NPU   reg   pass   test   input   encoding   bre   flush   

原文地址:https://www.cnblogs.com/jimGraymane/p/11544491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!