python 读写文件

## r表示读 rb表示读二进制 
file_object = open('test.py','r') try: file_text=file_object.read() finally: file_object.close() print file_text
## 可以直接用for循环遍历文件每行 file_input
= open('test.py', 'r') try: for line in file_input: print line finally: file_input.close()
## 或者使用readlines读取每行 file_input
= open('test.py','r') try: i=0; while( i <= 0): i=i+1 chunk = file_input.readlines() print(chunk) finally: file_input.close()

 

## w 表示写文件 w+ 写追加 wb 写二进制
infile = open('test.py','r') outfile = open('log.txt','w+') try: for line in infile: outfile.write(line) finally: infile.close() outfile.close()

 

参考链接:

http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html

posted @ 2012-12-04 11:37  lipeil  阅读(161)  评论(0编辑  收藏  举报