首页 > 其他 > 详细

文件(二)

时间:2018-06-27 15:37:10      阅读:247      评论:0      收藏:0      [点我收藏+]
读取文件

1、一次性读取整个文件file.read()

>>> fp = open("e:\\python\\hu.txt","r")
>>> content = fp.read()
>>> print content

看见
sfs
sfdds
1-1读取指定字节数file.read(count)
count为每次读取的字节数

>>> file_obj = open("e:\\python\\3.txt","r")
>>> file_obj.read(5)
‘123\n8‘

2、依次读取每行文件,存入一个列表,列表中的每个元素是一行file.readlines()

>>> file_obj = open("e:\\python\\3.txt","r")
>>> contents = file_obj.readlines()
>>> for content in contents:
...     print content
...

123
3、读取单行文件,file.readline()

>>> file_obj = open("e:\\python\\3.txt","r")
>>> b=file_obj.readline()
>>> b

‘123\n‘
4、利用文件对象名遍历文件

>>> file_obj = open("e:\\python\\3.txt","r")
>>> for cotent in file_obj:
...     print content
...

文件(二)

原文:http://blog.51cto.com/13496943/2133253

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!