zoukankan      html  css  js  c++  java
  • 作业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:UsersAdministratorDesktopa.txt',mode='r+',encoding='utf-8')as f:
        f.seek(2,0)
        print(f.read())
    # r+b   模式
    with open(r'C:UsersAdministratorDesktopa.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}
    ')
    
        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))
  • 相关阅读:
    如何通過編程獲取列表項目的附件以及多行文本中的文件內容
    小技巧:如何管理保存在本地的用户凭据
    WF 4.0中如何实现xaml工作流的动态加载
    使用jquery构造自己的多级菜单
    和安蕾尔的合影
    360太tmd脑残了
    3D 打印机技术设想
    再放2张数字油画
    解决问题的艺术:半小时编程实现照片的反转负冲特效
    承接数字油画图稿/线条图定制(出图)业务
  • 原文地址:https://www.cnblogs.com/jingpeng/p/12506820.html
Copyright © 2011-2022 走看看