读取文件
buffering 表示是否需要缓冲,默认-1
file_object = open(file,mode='r',buffering=-1)
写入文件
就要记得关文件不然后续数据容易丢失,采用with方法可以避免丢失
with open('test.txt','w') as file_object: file_object.write('hello world!')
写入一个字符串列表
writelines(se
#例如:
f = open('test.txt','w')
seq = ['hello','...','world']
f.writelines(seq) f.close()
文本文件读取
三种方式:
read([size]) 读取指定size的字符数,如果未给定size则读取所有,一整个字符串变量。
readline() 每次读取一行返回一个字符串对象
readlines() 读取每行作为一个字符串并存入列表,最终返回的是列表。