1、通用文件copy工具实现
src_file = input("输入文件路径:").strip()
new_file = input("输入新文件路径:").strip()
with open(f'{src_file}', mode="rb") as f1, \
open(f'{new_file}', mode="wb") as f2:
for line in f1:
f2.write(line)
2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
with open("a.txt", "r+", encoding="utf-8") as f:
print(f.read())
f.seek(6, 0)
print(f.tell())
f.write('1111\n222\n3333\n')
print(f.read())
with open("a.txt", "w+", encoding="utf-8") as f:
print(f.read())
f.seek(6, 0)
print(f.tell())
f.write('1111\n222\n3333\n')
print(f.read())
with open("a.txt", "a+", encoding="utf-8") as f:
print(f.read())
f.seek(6, 0)
print(f.tell())
f.write('1111\n222\n3333\n')
print(f.read())
3、tail -f access.log程序实现
cmd = input("请输入b文件追加的信息:").strip()
with open("b.txt", "a+t", encoding="utf-8") as f:
f.write(cmd)
with open("b.txt", "r", encoding="utf-8") as f:
print(f.read())