Python 学习day09 20171027 文件操作

一、Python open()函数文件打开操作

打开文件会用到open函数,语法如下:
open(name[,mode[,buffering]])
二、open()函数文件打开模式参数常用值有哪些?

参数mode的基本取值
Character    Meaning
‘r'    open for reading (default)
‘w'    open for writing, truncating the file first
‘a'    open for writing, appending to the end of the file if it exists
‘b'    binary mode
‘t'    text mode (default)
‘+'    open a disk file for updating (reading and writing)
‘U'    universal newline mode (for backwards compatibility; should not be used in new code)
r、w、a为打开文件的基本模式,对应着只读、只写、追加模式;
b、t、+、U这四个字符,与以上的文件打开模式组合使用,二进制模式,文本模式,读写模式、通用换行符,根据实际情况组合使用、

 

三、python文件写入操作

>>>f = open('a.txt', 'w')
>>>f.write('hello,')
>>>f.write('aaaaaaaaa')
>>>f.close()
第一行:用写的方式打开a.txt这个文件,并赋给f (python变量命名规则)
第二行:f.write方法写入( )括号内的内容
第三行:同第二行意义相同,重点要说明下f.write写入的内容会追加到文件中已存在的数据后,也就是就此时的'aaaaaaaaa'是在'hello,'后边显示的。
第四行:最后调用close方法关闭文件,有打开就要有关闭。
四、python文件读取操作方法

要进行读文件操作,只需要把模式换成'r'就可以,也可以把模式为空不写参数,也是读的意思,因为程序默认是为'r'的。
>>>f = open('a.txt', 'r')
>>>f.read(5)
'hello'
read( )是读文件的方法,括号内填入要读取的字符数,这里填写的字符数是5,如果填写的是1那么输出的就应该是‘h’。
打开文件文件读取还有一些常用到的技巧方法,像下边这两种:
1、read( ):表示读取全部内容
2、readline( ):表示逐行读取

文件对象的方法:
f.readline()   逐行读取数据
方法一:
>>> f = open('/tmp/test.txt')
>>> f.readline()
'hello girl!\n'
>>> f.readline()
'hello boy!\n'
>>> f.readline()
'hello man!'
>>> f.readline()
''
方法二:
>>> for i in open('/tmp/test.txt'):
...   print i
...
hello girl!
hello boy!
hello man!
f.readlines()   将文件内容以列表的形式存放
 
>>> f = open('/tmp/test.txt')
>>> f.readlines()
['hello girl!\n', 'hello boy!\n', 'hello man!']
>>> f.close()
f.next()   逐行读取数据,和f.readline() 相似,唯一不同的是,f.readline() 读取到最后如果没有数据会返回空,而f.next() 没读取到数据则会报错
>>> f = open('/tmp/test.txt')
>>> f.readlines()
['hello girl!\n', 'hello boy!\n', 'hello man!']
>>> f.close()
>>>
>>> f = open('/tmp/test.txt')
>>> f.next()
'hello girl!\n'
>>> f.next()
'hello boy!\n'
>>> f.next()
'hello man!'
>>> f.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
f.writelines()   多行写入
>>> l = ['\nhello dear!','\nhello son!','\nhello baby!\n']
>>> f = open('/tmp/test.txt','a')
>>> f.writelines(l)
>>> f.close()
[root@node1 python]# cat /tmp/test.txt
hello girl!
hello boy!
hello man!
hello dear!
hello son!
hello baby!
f.seek(偏移量,选项)
>>> f = open('/tmp/test.txt','r+')
>>> f.readline()
'hello girl!\n'
>>> f.readline()
'hello boy!\n'
>>> f.readline()
'hello man!\n'
>>> f.readline()
' '
>>> f.close()
>>> f = open('/tmp/test.txt','r+')
>>> f.read()
'hello girl!\nhello boy!\nhello man!\n'
>>> f.readline()
''
>>> f.close()
这个例子可以充分的解释前面使用r+这个模式的时候,为什么需要执行f.read()之后才能正常插入
f.seek(偏移量,选项)
(1)选项=0,表示将文件指针指向从文件头部到“偏移量”字节处
(2)选项=1,表示将文件指针指向从文件的当前位置,向后移动“偏移量”字节
(3)选项=2,表示将文件指针指向从文件的尾部,向前移动“偏移量”字节
偏移量:正数表示向右偏移,负数表示向左偏移

>>> f = open('/tmp/test.txt','r+')
>>> f.seek(0,2)
>>> f.readline()
''
>>> f.seek(0,0)
>>> f.readline()
'hello girl!\n'
>>> f.readline()
'hello boy!\n'
>>> f.readline()
'hello man!\n'
>>> f.readline()
''
f.flush()    将修改写入到文件中(无需关闭文件)
>>> f.write('hello python!')
>>> f.flush()
[root@node1 python]# cat /tmp/test.txt
hello girl!
hello boy!
hello man!
hello python!
f.tell()   获取指针位置
>>> f = open('/tmp/test.txt')
>>> f.readline()
'hello girl!\n'
>>> f.tell()
12
>>> f.readline()
'hello boy!\n'
>>> f.tell()
23

最常用:

 with open('test.txt','rb') as fd:

        data = fd.read()

        ......

posted @ 2017-10-28 11:51  木头爱木头媳妇  阅读(95)  评论(0)    收藏  举报