作业3月16号

1、通用文件copy工具实现

src_fire = input('复制路径》')
dsc_fire = input('粘贴路径》')
with open(r'{}'.format(src_fire),mode='rb')as f1,\
    open(r'{}'.format(dsc_fire),mode='wb')as f2:
    for msg in f1:
        f2.write(msg)

2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容

# r+t  模式
with open(r'C:\Users\Administrator\Desktop\a.txt',mode='r+',encoding='utf-8')as f:
    f.seek(2,0)
    print(f.read())
# r+b   模式
with open(r'C:\Users\Administrator\Desktop\a.txt',mode='r+b',)as f1:
    f1.seek(2,0)
    print(f1.read().decode('utf-8'))
# w+b   模式
with open(r'a.txt', mode='w+b', )as f2:
    f2.write(b'lsd123')
    f2.seek(2,0)
    print(f2.read())
    f2.write(bytes('',encoding='utf-8'))
    f2.seek(4,0)
    print(f2.read().decode('utf-8'))
# a+b 模式

with open(r'a.txt', mode='a+b', )as f3:
    f3.write(b'lsd123')
    f3.seek(3, 0)
    print(f3.read().decode('utf-8'))
    f3.write(bytes('上xia', encoding='utf-8'))
    f3.seek(5, 0)
    print(f3.read().decode('utf-8'))

3、tail -f access.log程序实现

import time
with open('access.log', 'rt', encoding='utf-8') as f:
    f.seek(0, 2)
    while True:
        res = f.readline()
        if not res:
            time.sleep(0.5)
            continue
        print(res)

4、周末作业参考在老师讲解完毕后(下午17:30开始讲解),练习熟练,明早日考就靠他俩

 4.1:编写用户登录接口

tag = True
while tag:
    username = input('请输入用户名: ').strip()
    with open('user_info4.txt', 'r', encoding='utf-8') as f, open('user_info5.txt', 'w', encoding='utf-8') as w:
        for line in f:
            user, pwd, locked = line.strip().split(':')
            # print(user, pwd, type(locked))
            locked = int(locked)

            if locked == 3:
                print('当前用户已被锁定')

            if username == user:
                while locked < 3:
                    password = input('请输入密码: ').strip()
                    if password == pwd:
                        print('登录成功!')
                        tag = False
                        break
                    else:
                        print('密码错误!')
                        locked += 1

            w.write(f'{user}:{pwd}:{locked}\n')

    import os
    os.remove('user_info4.txt')
    # 将user_info5.txt修改为user_info4.txt
    os.rename('user_info5.txt', 'user_info4.txt')


 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)

while True:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字,傻叉')
        continue

    if cmd not in dic:
        print('输入有误')
        continue

    print(dic.get(cmd))

猜你喜欢

转载自www.cnblogs.com/jingpeng/p/12506820.html
今日推荐