文件写入 & 读取

1、基本函数,不止是内部函数

io.open():

some_file=io.open('somefile.txt','w+')

.close():

.flush():刷新缓存,好像有时没用?

.read(n):读取字符个数

.readline():读取一行字符:也可以指定字符个数

.readlines():返回所有行;也可以指定字符个数

.write():接连写入字符

.writelines():写入字符串列表

some_file.writelines(['There is nothing here except
','This stupid haiku'])

print(.readlines())与pprint.pprint(.readlines())输出区别是啥?都可以输出字符串列表

自动关闭,无需close():pprint.pprint(open(r'C:	extsomefile.txt').readlines())

2、如何改变输出流位置?比如如何从头输出?

注意:

1)每次open打开,如果是‘w+’,直接清空;如果是‘r+’,则不会;如果是‘r+’,尽管可以修改内容,插入字符的位置为txt文件的第一行开头,附加内容是‘a+’;

原文地址:https://www.cnblogs.com/wllwqdeai/p/13198936.html