import sys,time
'''
#文件句柄
f = open("yesterday2",'a',encoding="utf-8")#'r'=read,'w'=write,w是创建文件.a=append追加的意思.默认读模式
# print(f.read())#文件光标读取一遍
f.write("我爱北京*.....\n")
f.write("*上太阳升.....")
#文件关闭
f.close()
'''
# f = open("yesterday",'r',encoding="utf-8")
# # print(f.readline())#只读一行
# for i in range(5):
# print(f.readline())#读前5行
#
# f.close()
# f = open("yesterday",'r',encoding="utf-8")
# for line in f.readlines():
# print(line.strip())
#low loop
# f = open("yesterday",'r',encoding="utf-8")
# for index,line in enumerate(f.readlines()):
# if index == 9:#取第10行数据
# print('-----分割线-----')
# continue
# print(line.strip())
#high bige
# count = 0
# f = open("yesterday",'r',encoding="utf-8")
# for line in f:#一行一行读,效率最高
#
# if count == 9:#取第10行数据
# print('-----分割线-----')
# count += 1
# continue
# print(line)
# count += 1
# f = open("yesterday",'r',encoding="utf-8")
# print(f.tell())#打印文件句柄的光标位置,按照字符计数
# print(f.readline())
# print(f.read(5))
# print(f.tell())
# f.seek(0)#光标回到0位置
# print(f.readline())
# print(f.tell())
# print(f.encoding)#打印文件编码
# print(f.fileno())
# print(f.isatty())#打印终端设备
# print(f.flush())#刷新,实时刷新到硬盘上
# f.truncate(10)#截断,从头开始截断。必须为a模式
# f = open("yesterday2",'r+',encoding="utf-8")#可读写
# # f1 = open("yesterday2",'w+',encoding="utf-8")#可写读
# # f2 = open("yesterday2",'a+',encoding="utf-8")#追加读
# # f3 = open("yesterday3",'rb')#二进制读,不能加encoding编码,网络传输必须用二进制打开
# f4 = open("yesterday3",'wb')#二进制写,不能加encoding编码,网络传输必须用二进制打开
# f4.write('hello world'.encode())#二进制写必须转换成str类型,不写就是默认utf-8模式
#
# #写读是先创建一个文件,读写是读取在后面添加写入
# print(f.readline())
# print(f.readline())
# print(f.readline())
# f.write('-------diao--------\n')
# print(f.read())
# f.close()
#flush 刷新输出
# for i in range(50):
# sys.stdout.write("*")
# sys.stdout.flush()
# time.sleep(0.1)
#文件修改
f = open("yesterday2",'r',encoding="utf-8")
f_new = open("yesterday4.bak",'w',encoding="utf-8")
for line in f :
if "我爱北京*" in line:
line = line.replace('我爱北京*','肆意的快乐等着cindy去享受')#old -- 将被替换的子字符串。new -- 新字符串,用于替换old子字符串。max -- 可选字符串, 替换不超过 max 次
print(line)
f_new.write(line)
f.close()
f_new.close()