3.16作业

主宰稳场 提交于 2020-03-17 10:24:16

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

# file_path = input('请输入源文件路径:').strip()
# # copy_path = input('复制后文件的路径:').strip()
# # with open(r'%s'%file_path,mode='rb') as f1,\
# # open(r'%s'%copy_path,mode='wb') as f2:
# # for item in f1:
# # f2.write(item)

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

r+模式:
with open(r'kkk.txt',mode='r+',encoding='utf-8') as f:
print(f.read())
f.seek(3,0)
print(f.tell())
f.write('345')
f.seek(0,0)
print(f.read())

w+模式:
with open(r'kkk.txt',mode='w+',encoding='utf-8') as f:
print(f.read())
f.seek(3,0)
print(f.tell())
f.write('123')
f.seek(0,0)
print(f.read())

a+模式:
with open(r'kkk.txt',mode='a+',encoding='utf-8') as f:
print(f.read())
f.seek(3,0)
print(f.tell())
f.write('123')
f.seek(0, 0)
print(f.read())

 

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

while True:
print('输入任意文字可添加至日志,输入readlog可读取日志信息')
msg = input('输入指令:').strip()
if msg == 'readlog':
with open(r'access.log', 'a+b') as f:
f.write(bytes('{}\n'.format(msg), encoding='utf-8'))
f.seek(0, 0)
log = f.read().decode('utf-8')
print(log)
continue
else:
with open(r'access.log', 'ab') as f:
f.write(bytes('{}\n'.format(msg), encoding='utf-8'))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!