码迷,mamicode.com
首页 > 其他好文 > 详细

Reading and writing

时间:2014-09-23 02:45:53      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   os   ar   for   2014   

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

标签:des   style   blog   http   io   os   ar   for   2014   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!