现在的位置: 首页 > 综合 > 正文

002_007 Python 随机输入/输出即随机读取文件

2018年02月15日 ⁄ 综合 ⁄ 共 255字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8


print '中国'


#随机输入输出
#一个大的二进制文件,读取某一条记录,不需要逐条读取
'''d:\123.txt的内容
1 a b c 中 国
2 a b c 中 国
'''
thefile = open(r'd:\123.txt','rb')
record_size = 2
record_num = 2
thefile.seek(record_num * record_size)


buffer = thefile.read(record_size)
print buffer
thefile.close()

打印结果如下:

中国

抱歉!评论已关闭.