• emmm......就当练习了系列08


    1、通用文件copy工具实现

     src_file=input('源文件路径>>: ').strip()
     dst_file=input('源文件路径>>: ').strip()
     with open(r'{}'.format(src_file),mode='rt',encoding='utf-8') as f1,
       open(r'{}'.format(dst_file),mode='wt',encoding='utf-8') as f2:
     # 方法一:  

      res=f1.read()
         f2.write(res)

    # 方法二:

      for line in f1:
        f2.write(line)

     

    2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容

    #r+

    with open(r'h.txt', 'r+b') as f:
    print(f.read().decode('utf-8')) # 读取源文件内容
    f.seek(0, 0) # 指针移动到开始位置
    f.write('哈利路亚'.encode('utf-8')) # 覆盖内容,写下'哈利路亚'
    f.seek(0, 0)
    print(f.read().decode('utf-8'))
    #w+
    with open(r'h.txt', 'w+b') as f:
    # w+模式打开,清空文本内容
    f.write(bytes('happy啦啦啦', encoding='utf-8'))
    f.write('yoyoyo切克闹'.encode('utf-8'))
    f.seek(5, 0)
    # 指针回到开始位置,并移动五个字节
    print(f.read().decode('utf-8'))
    # a+
    with open(r'h.txt', 'a+b') as f:
    f.write(' 233lalala '.encode('utf-8'))
    f.seek(-10, 2)
    f.seek(-5, 1)
    res2 = f.read().decode('utf-8')
    print(res2)


    3、tail -f access.log程序实现

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



  • 相关阅读:
    spring mvc ajax请求
    spring mvc 返回页面数据
    spring mvc 参数传递的三种方式
    spring mvc 注解示例
    spring mvc handler的三种方式
    MyBatis 一级、二级缓存
    MyBatis 调用存储过程
    MyBatis动态SQL与模糊查询
    spring mvc 注解入门示例
    MyBatis 一对多关联查询
  • 原文地址:https://www.cnblogs.com/lucky-cat233/p/12507194.html
Copyright © 2020-2023  润新知