python小作业第十二天

1、通用文件copy工具实现

src_file = input('请输入源文件路径:>>').strip()
copy_file = input('请输入文件保存的路径:>>').strip()
with open(r'{}'.format(src_file), 'rb') as f1, \
        open(r'{}'.format(copy_file), 'wb') as f2:
    for line in f1:
        f2.write(line)

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

# r+

with open(r'c.txt','rb+') as f:
    res = f.read().decode('utf-8')
    print(res)
    f.write('nb'.encode('utf-8'))
    f.seek(0,0)
    prt = f.read().decode('utf-8')
    print(prt)
# w+

with open(r'c.txt','wb+') as f1:
    f1.write('真的是太强了'.encode('utf-8'))
    f1.seek(9,0)
    prt = f1.read().decode('utf-8')
    print(prt)
# a+

with open(r'c.txt','ab+') as f2:
    f2.write('小老弟有操作的呀'.encode('utf-8'))
    f2.seek(15,0)
    prt = f2.read().decode('utf-8')
    print(prt)

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

import time
cmd = input('输入命令:>>').strip()
    if cmd == 'tail -f access.log':
        with open(r'access.log','rb') as af:
            af.seek(0,2)
            while True:
                prt = af.readline()
                if prt == 0:
                    time.sleep(2)
                else:
                    print(prt)

    else:
        print('还没有该日志,请重新输入')

posted @ 2020-03-16 17:34  Lance_王  阅读(143)  评论(0编辑  收藏  举报