作业3月17号

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改

def info():
    import os
    path = input('请输入路径:')
    front = input('请输入修改内容:')
    later = input('请输入修改后内容:')
    with open(path, 'r+', encoding='utf-8') as f1, \
            open('a.txt', 'w', encoding='utf-8')as f2:
        for x in f1:
            f2.write(x.replace(front, later))
    os.remove(path)
    os.rename('a.txt', path)


info()

 

# 2、编写tail工具

def tail():
    import time
    inp_tail = input('请输入监控路径:')
    with open(inp_tail, 'r', encoding='utf-8') as f3:
        while 1:
            res = f3.readline()
            if not res:
                time.sleep(0.2)
                continue
            print(res)


tail()

 

# 3、编写登录功能

def register():
    inp_name = input('请输入账号:')
    inp_pwd = input('请输入密码:')
    if inp_name == inp_pwd == '123':
        print('登录成功')
    else:
        print('登录失败')


register()

 

# 4、编写注册功能

def registration():
    inp_name = input('请输入账号:')
    inp_pwd = input('请输入密码:')
    with open('a.txt', 'a', encoding='utf-8')as f4:
        f4.write('{}:{}\n'.format(inp_name, inp_pwd))
registration()
posted @ 2020-03-17 21:29  疏星淡月  阅读(110)  评论(0编辑  收藏  举报