python中,可以通过open()函数打开一个文件创建一个file类的对象来操作文件,也可以在打开文件创建file对象时指定文件打开的模式(如果没有指定打开模式,默认为r),来决定能对文件进行的操作。这里说的文件读写操作就是利用file类中提供的read、readline、readlines和write等方法来操作文件。
1、read和write
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.
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.
read函数除非指定size,否则会默认读取文件的全部内容并返回。write函数会将一个str写入到文件当前写入内容的末尾(意思就是,f.write(str1), f.write(str2),str2会出现在str1的后面)。
2、打开模式
和其它编程语言类似,python中的文件打开模式也有三种:读模式('r')、写模式('w')和追加模式(‘a’)。读模式下打开的文件只能用来进行读操作;写模式下打开的文件能进行写操作,但是文件中原来的内容会被清空掉;追加模式下打开的文件能进行写操作,而且所有被写入的内容都会追加在原来文件的末尾。
2.1 写模式和追加模式
写模式和追加模式下打开的文件如果不存在,该文件会默认被创建,其内容为空。
(1) r mode: write to a file
$ cat file_w.py
filename = "temp.txt"
f = open(filename,"w")
f.write("hello, world.\n")
f.write("hi, python!\n")
f.close()
$ python file_w.py
$ cat temp.txt
hello, world.
hi, python!
(2) r mode: write to the same file again
$ cat file_w.py
filename = "temp.txt"
f = open(filename,"w")
f.write("Be serious!\n")
f.write("Not funny at all!\n")
f.close()
$ python file_w.py
$ cat temp.txt
Be serious!
Not funny at all!
可以看出,之前文件中的内容都被清空了。
(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!
2.2 读模式
对于读模式下打开的文件,可以进行读取操作。如果读取模式下打开一个不存在的文件,会报错(IOError: [Errno 2] No such file or directory)。
(1) read
像上文中说的read默认读取整个文件的内容并返回。
$ cat file_r.py
filename = "temp.txt"
f = open(filename,"r")
print f.read()
f.close()
$ python file_r.py
Be serious!
Not funny at all!
hello, world.
hi, python!
(2) 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.
读取文件的一行,保留换行符,到文件末尾返回空串。
$ cat file_readline.py
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()
$ cat temp.txt
Be serious!
Not funny at all!
hello, world.
hi, python!
//这里有一个空行
$ python file_readline.py
Be serious!
Not funny at all!
hello, world.
hi, python!
//这里最后也会打出空行
(3) 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.
重复调用readline读取文件中的所有行到一个list。
$ 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!
//这里最后也会打出空行
好了,到这里就可以用python进行文件的基本操作了,后面有复杂的需求会进一步补充。^_^