http://blog.csdn.net/xia7139/article/details/25544015
python中,可以通过open()函数打开一个文件创建一个file类的对象来操作文件,也可以在打开文件创建file对象时指定文件打开的模式(如果没有指定打开模式,默认为r),来决定能对文件进行的操作。这里说的文件读写操作就是利用file类中提供的read、readline、readlines和write等方法来操作文件。
1、read和write
- <strong>read([size]) -> read at most size bytes, returned as a string.</strong>
- 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.
- <strong>write(str) -> None. </strong>
- 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.
2、打开模式
和其它编程语言类似,python中的文件打开模式也有三种:读模式('r')、写模式('w')和追加模式(‘a’)。读模式下打开的文件只能用来进行读操作;写模式下打开的文件能进行写操作,但是文件中原来的内容会被清空掉;追加模式下打开的文件能进行写操作,而且所有被写入的内容都会追加在原来文件的末尾。
2.1 写模式和追加模式
写模式和追加模式下打开的文件如果不存在,该文件会默认被创建,其内容为空。
(1) r mode: write to a file
- $ cat file_w.py
- <span style="color:#663366;">filename = "temp.txt"
- f = open(filename,"w")
- f.write("hello, world.\n")
- f.write("hi, python!\n")
- f.close()</span>
- $ python file_w.py
- $ cat temp.txt
- <span style="color:#663366;">hello, world.
- hi, python!</span>
- $ cat file_w.py
- <span style="color:#663366;">filename = "temp.txt"
- f = open(filename,"w")
- f.write("Be serious!\n")
- f.write("Not funny at all!\n")
- f.close()</span>
- $ python file_w.py
- $ cat temp.txt
- <span style="color:#663366;">Be serious!
- Not funny at all!</span>
(3) a mode: write to the same file
- $ cat file_a.py
- filename = "temp.txt"
- f = open(filename,"a")
- f.write("hello, world.\n")
- f.write("hi, python!\n")
- f.close()
- $ python file_a.py
- $ cat temp.txt
- Be serious!
- Not funny at all!
- hello, world.
- hi, python!
对于读模式下打开的文件,可以进行读取操作。如果读取模式下打开一个不存在的文件,会报错(IOError: [Errno 2] No such file or directory)。
(1) read
像上文中说的read默认读取整个文件的内容并返回。
- $ cat file_r.py
- <span style="color:#663366;">filename = "temp.txt"
- f = open(filename,"r")
- print f.read()
- f.close()</span>
- $ python file_r.py
- <span style="color:#663366;">Be serious!
- Not funny at all!
- hello, world.
- hi, python!</span>
- 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.
- $ cat file_readline.py
- <span style="color:#663366;">filename = "temp.txt"
- f = open(filename,"r")
- line = f.readline()
- while line://最后的空行也会被打印出来
- #readline will retain an enter, so add ',' at the end to remove the enter of print
- print line,
- line = f.readline()
- f.close()</span>
- <span style="color:#663366;">$ cat temp.txt
- Be serious!
- Not funny at all!
- hello, world.
- hi, python!
- //这里有一个空行</span>
- $ python file_readline.py
- <span style="color:#663366;">Be serious!
- Not funny at all!
- hello, world.
- hi, python!
- //这里最后也会打出空行</span>
- <strong>readlines([size]) -> list of strings, each a line from the file.</strong>
- 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.
- $ cat file_readlines.py
- filename = "temp.txt"
- f = open(filename,"r")
- lines = f.readlines()
- for line in lines:
- print line,
- f.close()
- $ cat temp.txt
- Be serious!
- Not funny at all!
- hello, world.
- hi, python!
- //这里有一个空行
- $ python file_readlines.py
- Be serious!
- Not funny at all!
- hello, world.
- hi, python!
- //这里最后也会打出空行