zoukankan      html  css  js  c++  java
  • day12

    import os  # os: 用于与操作系统交互的模块

    # 1、通用文件copy工具实现
    '''
    while True:
    # 1)接收用户需要拷贝的源文件
    source_file = input('请输入源文件路径: ').strip()
    # 2) 判断源文件是否存在
    print(os.path.exists(source_file))
    # os.path.exists(文件路径) ---> 返回的结果如果是True证明文件存在,否则不存在
    # 若文件不存在,则让用户重新输入源文件路径
    if not os.path.exists(source_file):
    print('当前源文件路径不存在,请重新输入!')
    continue
    # 3) 让用户输入拷贝后的文件路径
    dst_file = input('请输入拷贝后的文件路径: ').strip()
    with open(source_file, 'rb') as f_r, open(dst_file, 'wb') as f_w:
    for line in f_r:
    f_w.write(line)
    break
    '''

    # 2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
    # r+: 可读可写
    # with open('test.txt', 'r+', encoding='utf-8') as f:
    # # 光标从文件文本开头,从左到右移动6个字节
    # f.seek(6)
    # # 读取: 4个字符
    # # res = f.read(4)
    # # print(res)
    #
    # # 写入: alex
    # f.write('alex')

    # w+: 可读可写,每次打开都会覆盖原来的内容,无法读取文件中的内荣。
    # with open('test.txt', 'w+', encoding='utf-8') as f:
    # res = f.read()
    # print(res)

    # a+: 可读可写,无论是a或者是a+模块,追加时都会从末尾追加,指针移动无效
    # with open('test.txt', 'a+', encoding='utf-8') as f:
    # # 指针移动后
    # f.seek(6)
    # # 读取4个字符
    # res = f.read(4)
    # print(res)
    # # 只能在文件文本中的末尾追加
    # f.write('tank_is_very_handsome!')

    # 3、tail -f access.log程序实现
    # tail -f access.log: 监听access.log文件末尾是否有追加的内容
    '''
    import time
    # 1) 读取文件的内容,循环监听
    with open('access.log', 'rb') as f:
    # 将指针移动到文件的最后一行的开头
    f.seek(0, 2)
    while True:
    line = f.readline() # 读取一行数据

    # 如果最后一行有数据则打印,否则继续监听
    if line:
    print(line.decode('utf-8'))

    else:
    # 否则等待,监听, 模拟延时操作
    time.sleep(0.2) # 相当于每0.2秒监听一次文件末尾是否追加了数据


    '''
    # 2、编写tail工具
    # 函数定义
    """
    def tail_util(log_path):
    '''
    :param log_path: 用于接收,需要监听的日志文件
    :return:
    '''
    import time

    with open(log_path, 'rb') as f:
    f.seek(0, 2)
    while True:
    line = f.readline()
    if line:
    print(line.decode('utf-8'))
    else:
    time.sleep(0.2)


    # 函数调用
    # tail_util('access.log')
    """


    # 4、周末作业参考在老师讲解完毕后(下午17:30开始讲解),练习熟练,明早日考就靠他俩
    # 4.1:编写用户登录接口
    # 1)讲用户所有的数据读取出来,存放在字典中
    '''
    user_info = {}
    with open('user_info.txt', mode='r', encoding='utf-8') as f:
    for line in f:
    user, pwd = line.strip().split(':')
    # user_info ---> {'tank': '123'}
    user_info[user] = pwd

    print('所有的用户数据为: ', user_info)
    while True:
    username = input('请输入用户名: ').strip()
    # 2) 校验用户名是否存在
    # 若 用户名不存在user_info字典中,则表示当前用户不存在
    if username not in user_info:
    print('当前用户不存在,请重新输入!')
    continue

    password = input('请输入密码: ').strip()

    if password == user_info.get(username):
    print('登录成功!')
    break
    else:
    print('登录失败!')
    '''

    # 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
    '''
    # 1) 读取文件中所有用户的数据
    user_info = {}
    with open('user_info.txt', mode='r', encoding='utf-8') as f:
    for line in f:
    user, pwd = line.strip().split(':')
    # user_info ---> {'tank': '123'}
    user_info[user] = pwd

    print('所有的用户数据为: ', user_info)
    while True:
    # 2) 让用户输入用户名与密码,进行注册
    username = input('请输入注册的用户名: ').strip()

    # 3) 判断当前用户是否存在,若存在则让其重新输入
    if username in user_info: # True, 证明用户已存在,让用户重新输入
    print('当前用户已存在,请重新输入!')
    continue

    # 4) 若用户不存在,则继续注册
    password = input('请输入密码: ').strip()
    re_password = input('请确认密码: ').strip()

    # 5)判断用户输入两次密码是否一致
    if password == re_password:
    print('注册成功!')

    # 6) 将注册成功的用户数据,追加到user_info.txt文件中
    with open('user_info.txt', 'a', encoding='utf-8') as f:
    f.write(f'{username}:{password} ')

    break

    else:
    print('两次密码不一致!')
    '''

  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/2722127842qq-123/p/12787500.html
Copyright © 2011-2022 走看看