file(name[, mode[, buffering]]) -> file object
Open a file. The mode can be ‘r’, ‘w’ or ‘a’ for reading (default),writing or appending. The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. Add a ‘b’ to the mode for binary files.
Add a ‘+’ to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size. The preferred way to open a file is with the builtin open() function.
Add a ‘U’ to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a ‘\n’ in Python. Also, a file so opened gains the attribute ‘newlines’;the value for this attribute is one of None (no newline read yet), ‘\r’, ‘\n’, ‘\r\n’ or a tuple containing all the newline types seen.
‘U’ cannot be combined with ‘w’ or ‘+’ mode.
file()与open()的功能一致,打开文件或创建文件。都属于内建函数。
file的属性和方法:
open(…)
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
open()函数是file()函数的别名函数,能够打开文件并返回一个文件对象而非文件的内容(应该理解为一个存储着文件的内容的对象,如果想获取内容便需要对文件对象进行操作)。可以指定不同的打开mode(rw),在调用open()函数后一定要调用文件对象内建的close()函数来关闭文件。一般结合try..finally语句来确定会关闭文件对象。
注意:当你open()一个文件,实质上是将该文件的内容加载到缓存中,所以当你open()文件之后,对文件做了修改也不会影响到open()返回的对象的value。
常用mode:
1. r(read缺省参数):已读的方式打开文件,不能调用write方法,当文件不存在时报错。
2. w(write):已写方式打开文件,能够写入内容并覆盖,不能调用read方法,如果文件不存在,则创建新同名文件。
3. a(append):已追加模式打开文件,可以进行写操作,如果恩健不存在,则创建同名文件。
4. +:使用+允许同时进行读写操作。
5. U:支持所有类型的换行符(\n、\r、\r\n)
6. b:表示对二进制文件进行操作(图片、视频)。
7. t:对文本文件进行操作。
6种mode可以组合使用
读文件
In [20]: pswd = open('/usr/local/src/pyScript/fileOperation.txt','r')
In [21]: type(pswd)
Out[21]: file
In [32]: pswd
Out[32]: <open file '/usr/local/src/pyScript/fileOperation.txt', mode 'r' at 0x7f048314a420>
1
2
3
4
5
6
7
8
9
read()读取所有文件内容
read(…)
read([size]) -> read at most size bytes, returned as a string.
If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was requested
may be returned, even if no size parameter was given.
读取指定Size的内容,缺省参数为全部内容,返回一个String类型对象。
readline(…)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.
读取文件中的一行含有行结束符的内容,每执行一次会自动获取往下一行的内容,返回一个String。当读取到最后一行再执行此函数时,会返回一个空String,不会报错。
In [62]: pwd = open('fileOperation.txt','r')
In [70]: content = pwd.readline()
In [71]: content
Out[71]: 'stack:x:1001:1001::/opt/stack:/bin/bash\n'
In [72]: content = pwd.readline()
In [73]: content
Out[73]: 'memcache:x:116:125:Memcached,,,:/nonexistent:/bin/false\n'
import os
def ergodicIndex(fileName):
pwd = open('fileOperation.txt','r')
try:
content = pwd.readline()
index = content.split(':')
for i in index:
print i,
finally:
pwd.close()
if __name__ == '__main__':
fileName='/usr/local/src/pyScript/fileOperation.txt'
if os.path.exists(fileName):
ergodicIndex(fileName)
else:print "The file not exist"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
在处理文件数据中是非常常用的一个方法
In [99]: %run testReadline.py
stack x 1001 1001 /opt/stack /bin/bash
1
2
readlines()读取所有文件内容
readlines(…)
readlines([size]) -> list of strings, each a line from the file.
Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.
获取文件所有的内容,并返回一个以每行内容作为一个String元素的List类型对象,本质是通过循环调用readline()实现的。
write(…)
write(str) -> None. Write string str to file.
Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written.
将传递的String参数写入并覆盖文件内容,返回None。需要执行close()或flush()后才会将内存的数据写入到文件中。
注意:当你在没有调用close()函数之前,你是可以调用多次write()函数来实现追加额效果,即后来的write()函数的写入的内容并不会覆盖前一次使用write()函数写入的内容,但是不会自动添加换行符。
import os
def write_test(fileName,content_iterable):
try:
pwd = open(fileName,'w')
for key,value in content_iterable.items():
pwd.write(key+'\t'+value+'\n') #传入String类型参数同时加入换行符
finally:
pwd.close()
if __name__ == '__main__':
fileName = '/usr/local/src/pyScript/fileOperation.txt'
dic = {'name':'Jmilk','age':'23','city':'BJ'}
if os.path.exists(fileName):
write_test(fileName,dic)
else:print 'File not exist!'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
结果:
city BJ
age 23
name Jmilk
1
2
3
writelines()写入多行内容
writelines(…)
writelines(sequence_of_strings) -> None. Write the strings to the file.
Note that newlines are not added. The sequence can be any iterable object
producing strings. This is equivalent to calling write() for each string.
将传递的迭代对象的String元素逐个写入文件,相当于没一行都调用额write()函数,但是不会自动添加换行符。
修改上面的综合例子:
if __name__ == '__main__':
fileName = '/usr/local/src/pyScript/fileOperation.txt'
li = ['my name is Jmilk'+'\n','My name is chocolate'+'\n'] #定义List时加入换行符
if os.path.exists(fileName):
write_lines(fileName,li)
else:print 'File not exist!'
tell(…)
tell() -> current file position, an integer (may be a long integer).
In [283]: pwd = open('fileOperation.txt','rw+')
In [285]: pwd.tell()
Out[285]: 0
1
2
3
4
truncate()截断文件
truncate(…)
truncate([size]) -> None. Truncate the file to at most size bytes.
Size defaults to the current file position, as returned by tell().
默认从文件指针指向的位置开始截断文件内容,也可以通过传递int参数n来指定截断的起始位置,即改变文件指针的位置。从文件指针指向的位置n开始,之后的文件内容(不包含n)全部删除,以可修改mode打开的文件可以使用此方法。
In [273]: cat fileOperation.txt
0123456789
In [274]: pwd = open('fileOperation.txt','rw+')
In [275]: pwd.truncate(5)
In [276]: pwd.close()
In [277]: cat fileOperation.txt
01234
1
2
3
4
5
6
7
8
9
10
11
seek()转移文件指针
seek(…)
seek(offset[, whence]) -> None. Move to new file position.
可以接收偏移量和选项作为参数,返回None。
当whence==0时,将文件指针从文件头部转移到”偏移量”指定的字符处。
当whence==1时,将文件指针从文件的当前位置往后转移”偏移量”指定的字符数。
当whence==2时,将文件指针从文件尾部向前移动”偏移量”指定的字符数。
一个综合例子:
truncate()+tell()+seek()