3.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('{} '.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('{} '.format(msg), encoding='utf-8'))

原文地址:https://www.cnblogs.com/haliluyafeng/p/12508590.html