标签:pytho utf-8 lap port seek stdout 硬盘 yesterday 使用场景
f = open("yesterday","r",encoding="utf-8") #print(f.read()) #for i in range(5): # print(f.readline()) #打印前5行 #low loop ‘‘‘ for index,line in enumerate(f.readline()): if index == 9: print(‘---------我是分割线---------‘) continue print(line.strip()) ‘‘‘ # high bige ‘‘‘ count =0 for line in f: if count==9: print(‘---------我是分割线-----‘) count +=1 continue print(line) #效率最高,一行一行的读 count +=1 ‘‘‘ f =open("yesterday2","w",encoding="utf-8") f.write("hello1\n") f.write("hello2\n") f.write("hello3\n") f.flush()#实现数据从缓存刷到硬盘 ‘‘‘ print(f.tell()) #查询光标位置 print(f.readline()) print(f.tell())#查询光标位置,按字符计数 print(f.read(5)) print(f.tell()) f.seek(0)#光标回到某个字符位置 print(f.readline()) f.seek(10) print(f.readline()) print(f.encoding)#打印文件编码 print(f.fileno()) #读取文件编号 #print(f.flush()) print(dir(f.buffer)) ‘‘‘ ‘‘‘#进度条实现 import sys,time for i in range(50): sys.stdout.write("#") sys.stdout.flush() time.sleep(0.1) ‘‘‘ f = open("yesterday","a+",encoding="utf-8")#追加读写 #f.truncate(10) #截断 f.seek(10) f.truncate(20) #可以打开,追加 f = open("yesterday","r+",encoding="utf-8")#读写 print(f.readline()) print(f.readline()) print(f.readline()) print(f.tell()) f.write(‘--------niu----------‘) #用处不大 f = open("yesterday","w+",encoding="utf-8")#写读 f.write(‘--------niu----------1\n‘) f.write(‘--------niu----------2\n‘) f.write(‘--------niu----------3\n‘) print(f.tell()) f.seek(10) #不能在中间插入,只能继续往后写,或者覆盖之前的 print(f.tell()) #使用场景:网络传输只能用二进制 f = open("yesterday","rb")#以二进制格式读文件 print(f.readline()) print(f.readline()) print(f.readline()) f = open("yesterday","wb")#以二进制格式写文件 f.write("hello binary\n".encode()) f = open("yesterday","ab")#以二进制格式追加文件
标签:pytho utf-8 lap port seek stdout 硬盘 yesterday 使用场景
原文地址:http://www.cnblogs.com/xiaobai005/p/7773235.html