## 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 linefinally: 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