python文件读写操作

http://blog.csdn.net/xia7139/article/details/25544015

python中,可以通过open()函数打开一个文件创建一个file类的对象来操作文件,也可以在打开文件创建file对象时指定文件打开的模式(如果没有指定打开模式,默认为r),来决定能对文件进行的操作。这里说的文件读写操作就是利用file类中提供的read、readline、readlines和write等方法来操作文件。

1、read和write

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <strong>read([size]) -> read at most size bytes, returned as a string.</strong>  
  2. 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.  
  3. <strong>write(str) -> None. </strong>  
  4. 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

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat file_w.py   
  2. <span style="color:#663366;">filename = "temp.txt"  
  3. f = open(filename,"w")  
  4. f.write("hello, world.\n")  
  5. f.write("hi, python!\n")  
  6. f.close()</span>  
  7. $ python file_w.py   
  8. $ cat temp.txt   
  9. <span style="color:#663366;">hello, world.  
  10. hi, python!</span>  
(2) r mode: write to the same file again
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat file_w.py   
  2. <span style="color:#663366;">filename = "temp.txt"  
  3. f = open(filename,"w")  
  4. f.write("Be serious!\n")  
  5. f.write("Not funny at all!\n")  
  6. f.close()</span>  
  7. $ python file_w.py   
  8. $ cat temp.txt   
  9. <span style="color:#663366;">Be serious!  
  10. Not funny at all!</span>  
可以看出,之前文件中的内容都被清空了。

(3) a mode: write to the same file

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat file_a.py   
  2. filename = "temp.txt"  
  3. f = open(filename,"a")  
  4. f.write("hello, world.\n")  
  5. f.write("hi, python!\n")  
  6. f.close()  
  7. $ python file_a.py   
  8. $ cat temp.txt   
  9. Be serious!  
  10. Not funny at all!  
  11. hello, world.  
  12. hi, python!  
2.2 读模式

对于读模式下打开的文件,可以进行读取操作。如果读取模式下打开一个不存在的文件,会报错(IOError: [Errno 2] No such file or directory)。

(1) read

像上文中说的read默认读取整个文件的内容并返回。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat file_r.py   
  2. <span style="color:#663366;">filename = "temp.txt"  
  3. f = open(filename,"r")  
  4. print f.read()  
  5. f.close()</span>  
  6. $ python file_r.py   
  7. <span style="color:#663366;">Be serious!  
  8. Not funny at all!  
  9. hello, world.  
  10. hi, python!</span>  
(2) readline

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. readline([size]) -> next line from the file, as a string.  
  2. 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.  
读取文件的一行,保留换行符,到文件末尾返回空串。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat file_readline.py   
  2. <span style="color:#663366;">filename = "temp.txt"  
  3. f = open(filename,"r")  
  4. line =  f.readline()  
  5. while line://最后的空行也会被打印出来  
  6.     #readline will retain an enter, so add ',' at the end to remove the enter of print  
  7.     print line,  
  8.     line = f.readline()  
  9. f.close()</span>  
  10. <span style="color:#663366;">$ cat temp.txt   
  11. Be serious!  
  12. Not funny at all!  
  13. hello, world.  
  14. hi, python!  
  15. //这里有一个空行</span>  
  16. $ python file_readline.py   
  17. <span style="color:#663366;">Be serious!  
  18. Not funny at all!  
  19. hello, world.  
  20. hi, python!  
  21. //这里最后也会打出空行</span>  
(3) readlines

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <strong>readlines([size]) -> list of strings, each a line from the file.</strong>      
  2. 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。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat file_readlines.py   
  2. filename = "temp.txt"  
  3. f = open(filename,"r")  
  4. lines =  f.readlines()  
  5. for line in lines:  
  6.     print line,  
  7. f.close()  
  8. $ cat temp.txt   
  9. Be serious!  
  10. Not funny at all!  
  11. hello, world.  
  12. hi, python!  
  13. //这里有一个空行  
  14. $ python file_readlines.py   
  15. Be serious!  
  16. Not funny at all!  
  17. hello, world.  
  18. hi, python!  
  19. //这里最后也会打出空行  
好了,到这里就可以用python进行文件的基本操作了,后面有复杂的需求会进一步补充。^_^http://blog.csdn.net/xia7139/article/details/25544015
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值