Python学习笔记024
读取光标位置
f=open('poem6', 'r', encoding='utf8')
# .tell显示当前光标位置,英文字母占1个位置,汉字占3个位置
print(f.tell())
data=f.read(10)
print(data)
print(f.tell())
# .seek移动光标到指定位置
f.seek(0)
print(f.read(10))
手动写入文件
f=open('poem7','w',encoding='utf8')
f.write('Hello world')
f.write('\nHello xy')
# .flush 把缓冲区的数据写入磁盘文件
f.flush()
f.close()
import sys,time
for i in range(30):
sys.stdout.write('*')
# sys.stdout.flush()
time.sleep(0.2)
for i in range(30):
print('%',end='',flush=True)
time.sleep(0.2)
f=open('poem7','w',coding='utf8')
f.truncate()
截取指定位置数据
f=open('poem7','a',encoding='utf8')
# .truncate默认从光标第一位开始,截取全部数据
f.truncate()
# .truncate默认从光标第一位开始截取,参数表示从截取的最后一位
f.truncate(200)
f.close()