20.03.17作业

1、通用文件copy工具实现

print("copy程序")
src_file = input("初始源文件路径:>>>")
copy_file = input("目标源文件路径:>>>")
with open(r"{}".format(src_file),mode="rb") as f1,\
    open(r"{}".format(copy_file),mode="wb") as f2:
    for line in f1:
        f2.write(line)
print("copy结束,欢迎下次再次使用")

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

# r+
with open('d.txt',mode='r+b')as f1:
    f1.seek(2,0)
    print(f1.read().decode('utf-8'))
    f1.seek(6,1)
    f1.write('stop'.encode('utf-8'))
# #w+
with open('d.txt',mode='w+b')as f2:
    f2.seek(0,1)
    print(f2.read().decode('utf-8'))
    f2.seek(1,1)
    f2.write('add'.encode('utf-8'))
#a+
with open('d.txt',mode='a+b')as f3:
    f3.seek(-5,2)
    print(f3.read().decode('utf-8'))
    f3.seek(3,1)
    f3.write('fgrg'.encode('utf-8'))
    f3.seek(1,1)
    f3.write('dgdsd'.encode('utf-8'))

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(1)
        else:
            print(line.decode("utf-8"),end=" ")
posted @ 2020-03-17 10:54  迎着阳光  阅读(77)  评论(0编辑  收藏  举报