登录、注册、删除小练习

#编写过程中遇到的问题:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 14-15: truncated uXXXX escape

window 读取文件可以用,但是在字符串中是被当作转义字符来使用,所以’d:a.txt’会被转义成’d:a.txt’这是正确路径,所以不会报错。而‘E:DSXLXday4username.txt'

中经过转义之后可能就找不到路径的资源了,例如:u可能转成unicode字符串了,所以导至路径找不到报错误;

两咱解决办法:1、在路径中加\;2、在路径前面加r:r'E:DSXLXday4username.txt'; 

r/R:非转义的原始字符串
与普通字符相比,其他相对特殊的字符,其中可能包含转义字符,即那些,反斜杠加上对应字母,表示对应的特殊含义的,比如最常见的” ”表示换行,” ”表示Tab等。而如果是以r开头,那么说明后面的字符,都是普通的字符了,即如果是“ ”那么表示一个反斜杠字符,一个字母n,而不是表示换行了。
以r开头的字符,常用于正则表达式,对应着re模块。

Python Error io.UnsupportedOperation: not readable:这个问题是因为模式写错误了;导致报错;

I/O operation on closed file  移动print的位置

这个模式

user_info = {} #存放所有的用户
with open('E:DSXLXday4\username.txt') as f:
    for line in f:
        # niuhanyang,123456

        line = line.strip()
        temp  = line.split(',')
        username = temp[0]
        pwd = temp[1]
        user_info[username]=pwd
for i in range(3):
    choice = input('请输入你的选择'
                   '1、登录 2、注册 3、删除').strip()
    if choice=='1':
        username = input('username:').strip()
        pwd = input('pwd:').strip()
        if username and pwd:
            if username in user_info:
                if user_info.get(username)==pwd: #get获取的value值=pwd
                    print('登录成功')
                else:
                    print('账号密码错误!')
            else:
                print("user not found!")
        else:
            print('账号密码不能为空!')
    elif choice=='2':
        username = input('username:').strip()
        pwd = input('pwd:').strip()
        cpwd = input('cpwd:').strip()
        if username and pwd and cpwd:
            if username in user_info:
                print('该用户已经被注册!')
            else:
                if pwd==cpwd:
                    user_info[username]=pwd
                    print('恭喜,注册成功!')
                    print(user_info)
                else:
                    print('两次输入的密码不一致!')
        else:
            print('不能为空!')
    elif choice=='3':
        username = input('username:').strip()
        if username:
            if username in user_info:
                user_info.pop(username)
                print('删除成功!')
        else:
            print('不能为空!')
    else:
        print("输入有误,请重新输入")
else:
    with open('users.txt','a+') as fw:
        fw.seek(0)
        for usname,pwd in user_info.items():
            fw.write(usname+','+pwd+'
')
原文地址:https://www.cnblogs.com/ruijie/p/10259238.html