19 to read and write files

1, the role of the file:

  The purpose of using a file, some data is to be stored together, allowing the next time a program executed directly, without having to re-create a copy, saving time and effort

2, write data (write)

  Use write () to write data to a file can be done in

  

 

 

   A.txt creates a file in the path "to read and write files .py" file is located after the operation in which the data is as follows:

  

  Note: If the file does not exist then create, if there are empty on the first, then write the data

3, the read data (Read)

  Use read (num) can read data from a file, NUM represents the length of the data read from the file (in bytes), if no incoming num, it means that all the data file is read

  

  Print Results:

 

  

 

  Note: If you just open the file with open, if you use the "r" may be omitted, that is write-only open ( "a.text")

4, the read data (the readlines)

  When read as no argument, readliness entire contents of file can be read in one time-line manner, and returns a list, where each data row as an element

  

 

     

 

 5, the read data (the readline)

  

 

     

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/shanlu0000/p/12326475.html