文件处理part1-练习👈

1、编写文件copy工具

import os
source_file = input("源文件>>:").strip()
new_file = input("目标文件>>:").strip()

# 判断源文件存不存在,不存在创建一个,防止下面读操作报错。
if not os.path.isfile(source_file):
    with open(r'{}'.format(source_file), mode='wt', encoding='utf-8') as f:
        for i in range(5):
            f.write('我擦嘞:{}\n'.format(i))
# 定制copy文件操作:读源文件,把源文件内容写到新文件中
with open(r'{}'.format(source_file), mode='rt', encoding='utf-8') as f_read, \
        open(r'{}'.format(new_file), mode='wt', encoding='utf-8') as f_write:
    for line in f_read:
        f_write.write(line)
# 打印出新文件与源文件内容,查看结果。
with open(r'{}'.format(source_file), mode='rt', encoding='utf-8') as f, \
        open(r'{}'.format(new_file), mode='rt', encoding='utf-8') as f1:
    print('源文件内容:'.center(50, '-'))
    print(f.read())

    print('新文件内容:'.center(50, '-'))
    print(f1.read())

2、编写登录程序,账号密码来自于文件

import os
account_file = r'account.txt'
# 判断用户账户文件存不存在,不存在创建一个
if not os.path.isfile(account_file):
    with open(account_file, mode='wt', encoding='utf-8') as f:
        f.write('alex:123\n')
        f.write('egon:123\n\n\n')

count = 0
while count < 3:
    inp_username = input("请输入账号>>:").strip()
    inp_password = input("请输入密码>>:").strip()

    # 打开账户文件,核验密码
    with open(account_file, mode='rt', encoding='utf-8') as f:
        for line in f:
            # 判断line是否为空行只有一个换行符
            if line == '\n':
                continue
            # 用strip去除文件内每一行的空白(包括换行符\n),用split切分出只含有用户名与密码的列表
            usernme, password = line.strip().split(':')
            if usernme == inp_username and password == inp_password:
                print('登录成功!')
                break
        else:
            print('登录失败【{}】次'.format(count + 1))  
            count += 1
else:
    print('输入三次错误,强制退出!')

3、编写注册程序,账号密码来存入文件

user_file = r'user.txt'
user_exist = False
while True:
    username = input("请输入账号>>:").strip()
    # 判断该用户是否存在,存在不让用户创建该账户
    import os
    if os.path.isfile(user_file):
        with open(user_file, mode='rt', encoding='utf-8') as f:
            for line in f:
                res = line.strip()
                #  判断文件是否为空行
                if not res:
                    continue
                # split切分出列表,取出第一个值:用户名
                if username == res.split(":")[0]:
                    print("用户已经存在,不能创建该账户")
                    user_exist = True
                    break
    if user_exist:
        continue

    password = input("请输入密码>>:").strip()
    # 判断账号是否是字母数字,且密码不为空
    if '' in [username, password] and username.isalnum():   #
        print("账号必须是字母和数字,且密码不能为空,请重新输入!")
        continue

    confirm_password = input("请确认密码>>:").strip()
    # 确认2次密码一致性
    if confirm_password != password:
        print('2次密码输入不一致,重新输入!')
        continue

    # a模式,没有该文件创建,有该文件追加写,文件指针默认跳转到文件末尾。
    with open(user_file, mode='at', encoding='utf-8') as f:
        f.write('{}:{}\n'.format(username, password))
        print("正在注册,亲稍等".center(50, '-'))
        import time
        time.sleep(2)
        print('恭喜【{}】!注册成功!'.center(50, '-').format(username))
    break
posted @ 2020-03-13 23:10  给你加马桶唱疏通  阅读(240)  评论(0编辑  收藏  举报