• python入门作业——文件修改、函数的登录、注册、tail


    题目一:编写文件修改功能

    方法一:文本编辑采用的就是这种方式
    实现思路:将文件内容发一次性全部读入内存,然后在内存中修改完毕后再覆盖写回原文件
    优点: 在文件修改过程中同一份数据只有一份
    缺点: 会过多地占用内存

    def modify(modify_path, old_content, new_content):
        '''文件修改'''
        with open('{}'.format(modify_path),mode='rt',encoding='utf-8') as f:
            res=f.read()
            data=res.replace(old_content,new_content)
            print(data)
        with open('{}'.format(modify_path),mode='wt',encoding='utf-8') as f1:
            f1.write(data)
    
    
    modify_path = input('输入要修改的文件路径:')
    old_content = input('输入要修改的内容:')
    new_content = input('输入修改后的内容:')
    modify(modify_path, old_content, new_content)
    

    方法二
    实现思路:以读的方式打开原文件,以写的方式打开一个临时文件,一行行读取原文件内容,修改完后写入临时文件...,删掉原文件,将临时文件重命名原文件名
    优点: 不会占用过多的内存
    缺点: 在文件修改过程中同一份数据存了两份

    import os
    
    
    def modify(modify_path, old_content, new_content):
        '''文件修改'''
        with open(r'{}'.format(modify_path), mode='rt', encoding='utf-8') as f1, 
                open(r'.{}.swap'.format(modify_path), mode='wt', encoding='utf-8') as f2:
            for line in f1:
                f2.write(line.replace(old_content, new_content))
        os.remove(r'{}'.format(modify_path))
        os.rename(r'.{}.swap'.format(modify_path), r'{}'.format(modify_path))
    
    
    modify_path = input('输入要修改的文件路径:')
    old_content = input('输入要修改的内容:')
    new_content = input('输入修改后的内容:')
    modify(modify_path, old_content, new_content)
    

    题目二:用函数编写tail工具

    import time
    def tail():
        with open('access.log', mode='rb') as f:
            # 1、将指针跳到文件末尾
            # f.read() # 错误
            f.seek(0, 2)
    
            while True:
                line = f.readline()
                if len(line) == 0:
                    time.sleep(5)
                else:
                    print(line.decode('utf-8'), end='')
    tail()
    

    题目三:用函数编写登录功能

    def login():
        count = 0
        count_1 = 3
        user_name = input('输入账号:').strip()
        with open('db.txt', mode='rt', encoding='utf-8') as f:
            for i in f:  # 定义文本中的账号密码
                info_name, info_pwd = i.strip().split(':')
                if user_name == info_name:
                    with open('black.txt', mode='r+t', encoding='utf-8') as f1:
                        for n in f1:
                            # print( user_name,n,end='') # 验证输入账号跟黑名单账号
                            name1 = n.strip()  # 因为文件中每一行默认有个换行'
    ' 所以要加strip去空格
                            if user_name == name1:
                                print('该账号被锁定')
                                break
                        else:
                            print('
    该账号没有被锁定,可以输入密码')
                            while count < 3:
                                print('你还有%s次机会' % (count_1))
                                user_pwd = input('输入密码:')
                                if user_pwd == info_pwd:
                                    print('登录成功')
                                    break
                                else:
                                    print('密码错误')
                                    count += 1
                                    count_1 -= 1
                            else:
                                print('密码错误过多,该账号已被锁定')
                            with open('black.txt', mode='at+', encoding='utf-8') as f2:
                                f2.write('
    %s' % (user_name))
                    break
            else:
                print('没有该账号')
    login()
    

    题目四:用函数编写注册功能

    def creat():
        name = input('注册账号:')
        password = input('注册密码:')
        with open('db.txt', mode='r+t', encoding='utf-8') as f:
            for i in f:
                info_name, info_pwd = i.strip().split(':')
                if name == info_name:
                    print('账号已注册')
                    break
            else:
                with open('db.txt', mode='at', encoding='utf-8') as n:
                    n.write('{}:{}
    '.format(name, password))
                    print('完成注册')
    
        user = input('请输入用户名:')
        pwd = input('请输入密码:')
        with open(r'user_info.txt', 'a', encoding='utf-8') as f:
            f.write('{}:{}
    '.format(user, pwd))
            print('注册成功')
    creat()
    
  • 相关阅读:
    每日总结
    团队冲刺9
    团队冲刺8
    团队冲刺7
    团队冲刺6
    团队冲刺5
    团队冲刺4
    团对冲刺3
    团队冲刺2
    每日博客
  • 原文地址:https://www.cnblogs.com/liuxinging/p/12513593.html
Copyright © 2020-2023  润新知