• 作业0316


    1、通用文件copy工具实现

    src_file=input(r'源文件路径: ').strip()
    dst_file=input(r'目标文件路径: ').strip()
    with open(r'{}'.format(src_file),mode='rb') as f1,open(r'{}'.format(dst_file),mode='wb') as f2:
        for line in f1:
            f2.write(line)
    

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

    作业2测试.txt用utf-8编码,内容如下(CHNwinUSAout各占1个字节,中文“是真的”各占3个字节)CHNwinUSAout是真的
    
    with open('作业2测试.txt',mode='r+',encoding='utf-8') as f:
        f.seek(6,0)     # 参照文件开头移动了6个字节
        print(f.tell()) # 查看当前文件指针距离文件开头的位置,输出结果为6
        print(f.read()) # 从第6个字节的位置读到文件末尾,输出结果为:USAout是真的
    
    with open('作业2测试.txt',mode='w+',encoding='utf-8') as f:
        f.seek(6,0)     # 参照文件开头移动了6个字节
        print(f.tell()) # 查看当前文件指针距离文件开头的位置,输出结果为6
        print(f.read()) # 从第6个字节的位置读到文件末尾,输出结果为:空,文本被w模式清空
    
    with open('作业2测试.txt',mode='a+',encoding='utf-8') as f:
        f.seek(6,0)     # 参照文件开头移动了6个字节
        print(f.tell()) # 查看当前文件指针距离文件开头的位置,输出结果为6
        print(f.read()) # 从第6个字节的位置读到文件末尾,输出结果为:空,文本被w模式清空
    

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

    import time
    with open('access.log',mode='rb') as f:
        f.seek(0,2)
        while True:
            line=f.readline()
            if len(line) == 0:
                time.sleep(0.5)
            else:
                print(line.decode('utf-8'),end='')
    
  • 相关阅读:
    vue.js 初步学习
    IntelliJ IDEA 快捷键
    SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因
    JavaScript基础概念与语法
    python 常见矩阵运算
    利用matplotlib的plot函数实现图像绘制
    基于密度峰值的聚类(DPCA)
    极角排序
    HDU-5514 Frogs (容斥)
    HDU
  • 原文地址:https://www.cnblogs.com/zuiyouyingde/p/12506859.html
Copyright © 2020-2023  润新知