• 文件的两种修改方式、函数的基本使用练习


    # 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
    '''
    import os
    def file(d,old,new):
    with open(d,mode='rt',encoding='utf-8') as f1,open('n.txt',mode='wt',encoding='utf-8') as f2:
    for line in f1:
    f2.write(line.replace(old,new))
    os.remove(d)
    os.rename('n.txt',d)

    file('that girl.txt','girl','a girl')
    '''
    # 2、编写tail工具
    '''
    import time
    def file(file,times):
    with open(file, mode='rb') as f:
    f.seek(0, 2)
    while True:
    i = f.read()
    if len(i) == 0:
    time.sleep(times)
    else:
    print(i.decode('utf-8'), end='')
    '''
    # 3、编写登录功能
    '''
    def users(file,users,pas):
    with open(file,mode='rt',encoding='utf-8') as f:
    for line in f:
    user,word = line.split(':')
    if users == user and pas == word:
    print('登录成功!')
    else:
    print('登录失败')

    users('userinfo.txt','tank','123')
    '''
    # 4、编写注册功能
    '''
    def users(file,users,pas):
    with open(file,mode='at',encoding='utf-8') as f:
    f.write('{}:{} '.format(users, pas))

    users('userinfo.txt','egon','123')
    '''
  • 相关阅读:
    js 练习,点击计算三个数的最大值,省级联动
    CSS 笔记
    CSS练习
    Html 学习笔记
    MySQL 执行计划详解
    别人家的元数据系统是怎么设计的
    深入浅出Dubbo RPC
    算法的时间复杂度和空间复杂度详解
    序列化 & 反序列化
    MySQL的四种隔离级别
  • 原文地址:https://www.cnblogs.com/0B0S/p/12511730.html
Copyright © 2020-2023  润新知