# 文件的基本方法以及对内容的迭代 # 1.open,write and read >>> f = open(r'E:\code\py\file\test.txt','w') >>> f.write('hello, ') 7 >>> f.write('world') 5 >>> f.close() >>> f = open(r'E:\code\py\file\test.txt','r') >>> f.read(4) 'hell' >>> f.read() 'o, world' >>> f.read() '' >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> f.read() 'hello, world' >>> f.close() >>> f = open(r'E:\code\py\file\test.txt','w') >>> f.write('hello,huhu') 10 >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> f.read() 'hello,huhu' >>> f.close() # 2.seek and tell >>> f = open(r'E:\code\py\file\test.txt','w') >>> f.write('0123456789') 10 >>> f.seek(5) 5 >>> f.write('hello,huhu') 10 >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> f.read() '01234hello,huhu' >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> f.read(4) '0123' >>> f.seek(3) 3 >>> f.read() '34hello,huhu' >>> f.tell() 15 >>> f.seek(5) 5 >>> f.tell() 5 >>> f.read(4) 'hell' >>> f.tell() 9 # 3.with statement >>> with open(r'E:\code\py\file\test.txt','w') as f: f.write('hello, 1234567890') 17 >>> with open(r'E:\code\py\file\test.txt','r') as f: f.read() 'hello, 1234567890' # 4.write, writelines >>> f = open(r'E:\code\py\file\test.txt','w') >>> f.write('Welcome to this file\nThere is nothing here except\nThis stupid haiku\n') 68 >>> f.close() >>> with open(r'E:\code\py\file\test.txt','w') as f: for i in l: f.write(i) 21 29 18 >>> with open(r'E:\code\py\file\test.txt','w') as f: f.writelines(l) # 5.read,readline and readlines # 5.1 read(n) and read() >>> f = open(r'E:\code\py\file\test.txt') >>> f.read(7) 'Welcome' >>> f.read(5) ' to t' >>> f.read() 'his file\nThere is nothing here except\nThis stupid haiku\n' >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> f.read() 'Welcome to this file\nThere is nothing here except\nThis stupid haiku\n' >>> f.read() '' >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> print(f.read()) Welcome to this file There is nothing here except This stupid haiku >>> f.close() # 按照字节遍历文件内容 >>> f = open(r'E:\code\py\file\test.txt') >>> while True: c = f.read(1) if len(c) == 0: break print(c, end = '') Welcome to this file There is nothing here except This stupid haiku >>> f.close() # 读取所有内容(整个文件当做一个字符串) # 1 >>> with open(r'E:\code\py\file\test.txt') as f: while True: line = f.read() if len(line) == 0: break print(line) Welcome to this file There is nothing here except This stupid haiku >>> # 2 >>> with open(r'E:\code\py\file\test.txt') as f: for c in f.read(): print(c, end = '') Welcome to this file There is nothing here except This stupid haiku >>> # 5.2 f.readline() >>> f = open(r'E:\code\py\file\test.txt') >>> f.readline() 'Welcome to this file\n' >>> f.readline() 'There is nothing here except\n' >>> f.readline() 'This stupid haiku\n' >>> f.readline() '' >>> f.close() # 按照行遍历文件内容 >>> with open(r'E:\code\py\file\test.txt') as f: while True: line = f.readline() if len(line) == 0: break print(line) Welcome to this file There is nothing here except This stupid haiku >>> >>> with open(r'E:\code\py\file\test.txt') as f: while True: line = f.readline() if len(line) == 0: break print(line, end = '') Welcome to this file There is nothing here except This stupid haiku >>> #5.3 readlines() >>> f = open(r'E:\code\py\file\test.txt') >>> f.readlines() ['Welcome to this file\n', 'There is nothing here except\n', 'This stupid haiku\n'] >>> f.readlines() [] >>> f.close() >>> f = open(r'E:\code\py\file\test.txt') >>> import pprint >>> pprint.pprint(f.readlines()) ['Welcome to this file\n', 'There is nothing here except\n', 'This stupid haiku\n'] >>> f.close() # 读取所有内容(整个文件当做一个字符串列表) >>> with open(r'E:\code\py\file\test.txt') as f: for line in f.readlines(): print(line) Welcome to this file There is nothing here except This stupid haiku >>> with open(r'E:\code\py\file\test.txt') as f: for line in f.readlines(): print(line, end = '') Welcome to this file There is nothing here except This stupid haiku >>>