zoukankan      html  css  js  c++  java
  • day12--作业

    1、通用文件copy工具的实现

    #通用文件拷贝
    def main(s,o):
        file_1 = open(r'{}'.format(s),mode='rb')
        file_2 = open(r'{}'.format(o),mode='wb')
        while True:
              res = file_1.read(1024)
              file_2.write(res)
              if not res:
                    break
    
    if __name__ == '__main__':
        source_path = input('请输入源文件地址:').strip()
        object_path = input('请输入目的地址:').strip()
        main(source_path,object_path)

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

    # 0:针对文件开头进行操作
    # 1:针对文件指针当前的位置进行操作
    # 2:针对文件尾部进行操作
    
    # rb+模式
    [root@Surpass day12]# python practice_2.py 
    请输入文件操作选择(rb+,wb+,ab+):rb+
    当前文件指针的位置:3
    [b'456xe5x93x88xe5x93x88xe6x88x91xe6x9dxa5xe4xbax86xe5xb0xb1xe8xbfx99xe6xa0xb7xe5x90xa7
    ']
    当前文件指针的位置:34
    当前文件指针的位置:40
    当前文件指针的位置:46
    当前文件指针的位置:50
    
    # wb+模式
    [root@Surpass day12]# python practice_2.py 
    请输入文件操作选择(rb+,wb+,ab+):wb+
    当前文件指针的位置:3
    []
    当前文件指针的位置:3
    当前文件指针的位置:9
    当前文件指针的位置:15
    当前文件指针的位置:19
    [root@Surpass day12]# 
    
    
    # ab+模式
    [root@Surpass day12]# python practice_2.py 
    请输入文件操作选择(rb+,wb+,ab+):ab+
    当前文件指针的位置:3
    [b'x00x00x00x00x00x00xe5x93x88xe5x93x88']
    当前文件指针的位置:15
    当前文件指针的位置:21
    当前文件指针的位置:27
    当前文件指针的位置:25
    [root@Surpass day12]# 

    实现的部分代码:

    self.f = open(self.testfile,self.mode)
        self.f.seek(3,0)
        self.position()
        print(self.f.readlines())
        self.position()
        self.f.seek(6,1)
        self.position()
        self.f.write('哈哈'.encode('utf-8'))
        self.position()
        self.f.seek(4,2)
        self.position()

    3、tail -f xxxx.log函数的实现

    tailf -10 xxxx.log 显示日志信息的最后10行

    import time
    
    class Tail:
        def __init__(self,filepath):
            self.filepath = filepath
            self.run()
    
        def run(self):
            try:
               self.f = open(r'{}'.format(self.filepath),mode='rb')
               self.f.seek(0,2)
               while True:
                   res = self.f.readline()
                   if not res:
                       time.sleep(0.5)
                   else:
                       print(res.decode('utf-8'),end='')
            except Exception as e:
                print('error msg:{}'.format(e))
    
    def main():
        filepath = input('请输入要查看的日志路径:').strip()
        Tail(filepath)
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    初心,勇敢~
    京东云,100倍故障时长赔付,呵呵
    京东万象数据接口,钱没花完,接口404,客服是白痴,无法维权
    随便写点
    关于UltraEdit的两个小问题
    Java
    2016年12月12日 回忆录随笔----------记录庸碌无为的一年四个月零十一天(一)
    iOS开发学习笔记
    sprintf函数php的详细使用方法
    BMS开发日记
  • 原文地址:https://www.cnblogs.com/surpass123/p/12505884.html
Copyright © 2011-2022 走看看