光标操作,with
获取光标的位置tell()
获取光标的位置,单位是字节
s1=open('简单',mode='r',encoding='utf-8')
coun2=s1.read()
print(s1.tell())
s1.close()
调整光标的位置seek()
s1=open('简单',mode='r',encoding='utf-8')
print(s1.seek(3))
coun2=s1.read()
print(coun2)
s1.close()
强制刷新,相当于保存 flush()
s1=open('简单',mode='w',encoding='utf-8')
coun2=s1.write('wowowowo')
s1.flush()
print(coun2)
s1.close()
文件打开的另一种方式with
-
优点
-
不用手动关闭文件句柄
with open('简单',mode='r',encoding='utf-8') as f1: #f1文件句柄 print(f1.read())
-
可以同时操作多个文件
with open('简单',mode='r',encoding='utf-8') as f1 ,\ #\换行 open('你简单,世界就是童话',encoding='utf-8',mode='w')as f2: print(f1.read()) print(f2.write('aaaaaa'))
-