zoukankan      html  css  js  c++  java
  • 作业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='')
    
  • 相关阅读:
    WebRequest 超时不起作用
    nutch导入ide nutch1.3
    nutch 设置抓取间隔策略
    c#: Error: Crossthread operation not valid
    mysql命令行基本操作
    Boost String Algorithms Library 函数详解三 (find)
    boost::timer 的替代方案
    windows驱动程序开发初探
    深入浅出Win32多线程程序设计之线程通信
    深入浅出Win32多线程设计之MFC的多线程(1)
  • 原文地址:https://www.cnblogs.com/zuiyouyingde/p/12506859.html
Copyright © 2011-2022 走看看