首页 > 其他 > 详细

Reading and writing

时间:2014-09-23 00:30:24      阅读:348      评论:0      收藏:0      [点我收藏+]

A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory, or CD-ROM. To read a file, you can use open to create a file object:

  bubuko.com,布布扣                     

Mode ‘r’ means that file is open for reading. The file object provides several methods for reading data, including readline.

The file object keeps track of where it is in the file, so if you invoke readline again, it picks up from where it left off. You can also use a file object in a for loop.

To write a file, you have to create a file object with mode ‘w’ as a second parameter. If the file already exists, opening it in write mode clears out the old data and starts fresh. If the file doesn’t exist, a new one is created. The write method puts data into the file. Again, the file object keeps track of where it is, so if you call write again, it add the new data to the end. When you done writing, you have to close the file.

 

 bubuko.com,布布扣

 

The write function returns the length of the string, including the ‘\n’ at the end of the string.

 

from Thinking in Python

Reading and writing

原文:http://www.cnblogs.com/ryansunyu/p/3987304.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!