首页 > 技术文章 > 3.16作业

cainiaoqianxun 2020-03-16 22:44 原文

3.16作业

1、通用文件copy工具实现

while True:
    src_path = input('源文件地址>>: ')
    dst_path = input('源文件地址>>: ')
    with open('{}'.format(src_path), mode='rb') as f1, \
            open('{}'.format(dst_path), mode='wb') as f2:
        for line in f1:
            f2.write(line)
        break

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

文件a.txt内容

aaa上中下
#r+模式测试读写
with open('a.txt', mode='r+b') as f:
    f.seek(3, 0)
    f.seek(3, 1)
    f.seek(-12, 2)
    print(f.tell())
    print(f.read().decode('utf-8'))
    f.seek(3, 0)
    f.seek(3, 1)
    f.seek(-3, 2)
    print(f.tell())
    f.write('优良差'.encode('utf-8'))
    f.seek(0, 0)
    f.seek(3, 1)
    f.seek(-12, 2)
    print(f.tell())
    res = f.read().decode('utf-8')
    print(res)
#w+模式测试读写
with open('a.txt', mode='r+b') as f:
    f.write('aaa优良差'.encode('utf-8'))
    f.seek(0, 0)
    f.seek(3, 1)
    f.seek(-9, 2)
    print(f.tell())
    res = f.read().decode('utf-8')
    print(res)
# a+模式测试读写
with open('a.txt', mode='r+b') as f:
    f.seek(3, 0)
    f.seek(3, 1)
    f.seek(-12, 2)
    print(f.tell())
    print(f.read().decode('utf-8'))
    f.seek(3, 0)
    f.seek(3, 1)
    f.seek(-3, 2)
    print(f.tell())
    f.write('优良差'.encode('utf-8'))
    f.seek(3, 0)
    f.seek(3, 1)
    f.seek(-18, 2)
    print(f.tell())
    res = f.read().decode('utf-8')
    print(res)

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

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

4.1:编写用户登录接口

print('<<登录窗口>>')
username = input('输入用户名: ')
password = input('输入密码: ')
print('当前用户{}、密码{}'.format(username, password))

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

pwd_info = {
    '0': '退出',
    '1': '登录',
    '2': '注册'
}
msg = '''
    0:退出
    1:登录
    2:注册
'''
tage = True
while tage:
    print(msg)
    pwd = input('请输入命令>>: ').strip()
    if not pwd.isdigit():
        print('老哥输入数字!')
        break
    if pwd not in pwd_info:
        print('输入命名不存在!')
        continue
    if pwd in pwd_info:
        if pwd == '0':
            break
        if pwd == '1':
            with open('a.txt', mode='rt', encoding='utf_8') as f1:
                inp_name = input('输入账号: ').strip()
                inp_password = input('输入密码: ').strip()
                for line in f1:
                    username, password = line.strip().split(':')
                    if inp_name == username and inp_password == password:
                        print('登录成功')
                        tage = False
                        break

                else:
                    print('密码错误')
        elif pwd == '2':
            with open('a.txt', mode='at', encoding='utf_8') as f2:
                inp_name = input('输入账号: ').strip()
                inp_password = input('输入密码: ').strip()
                f2.write('{}:{}\n'.format(inp_name, inp_password))

推荐阅读