when learning the python file read and write, because the tutorial is for Python2, and the use of Python3. When you want to take advantage of the file class, you can't find it in the class library, and reloading the python2 is not going to make it. In other people's garden carefully read the "detailed python2 and Python3 difference " (has been collected), only to find that Python3 has removed the file class.
Now the way to read and write files using Python is more similar to the C language of file read and write operations.
Now summarized as follows:
One open File--f = open (' Poem.txt ', ' x+ '):
Read the open Help document, and then translate it yourself, and now share some excerpts of the comments:
1 Open (...) Function: Opens the file and returns a data stream. An error is turned on to return IOError.
2 ways to open a file: You can open the text or bitstream (that is, the file) or wrap an integer file descriptor (??) through the file name (if it is not in the current directory). English is the integer file descriptor, in doubt).
3 Open File mode: The default way to open the file is ' RT '
Letter Meaning
--------- ---------------------------------------------------------------
' R ' read-only mode open (default mode)
The ' W ' write-only mode opens, first truncates the file (?? What is a truncated file? Doubt. )
' x ' creates a new file and opens (generates a FILEEXISTSERROR error if the file already exists)
' A ' read the file, if the file exists, append at the end of the file
' B ' binary read-write mode
' t ' text read/write mode (default)
' + ' opens a disk file for update (read and write operations)
' U ' universal line break mode (deprecated, newline value can be none, ' ', ' \ n ', ' \ R ', and ' \ r \ n ')
========= ===============================================================
4 Open (...) How to use:
File = open (file, mode= ' R ', Buffering=-1, Encoding=none,errors=none, Newline=none, Closefd=true, Opener=none)
(The following six parameters are optional)
Two close files--f.close ()
Always remember to call the close () method of the file object after opening the file with open.
You can close the file directly by calling Close ().
You can also use the try/finally statement to make sure that you can close the file finally. (Reference: http://jingyan.baidu.com/article/aa6a2c14d54f7f0d4c19c487.html)
File_object = open ('thefile.txt') try: = File_ Object.read () finally: file_object.close ()
Note: The Open statement cannot be placed in the try block because the file object File_object cannot execute the close () method when an exception is opened to the file.
Three-read file--file.read (size)
1 file.read (n) specify arguments, read n characters or bytes from the current position
2 file.read () do not specify parameters, read entire file
3 file.readline () reads a line of string, which ends with a line terminator ' \ n ', if it returns an empty string indicating that it has reached the end of the file, and if a ' \ n ' is returned, it is a blank line.
4 file.readlines () reads each line in the file, and each line of characters consists of a string. If you want to read all the rows of a file, you can use the list (f) function or f.readlines ().
Four write Files--file.write ()
file.write (String) writes the contents of a string to a file, returns a numeric value, and is the number of characters written. If you want to write other objects to a file, first convert them to a string.
Five files read the position of the pointer
File.tell () returns the position of the current file read-write pointer, if it is opened with binary, then this position represents the number of bytes from the beginning of the file header to the current position, if it is in text mode, then the meaning of this position is more blurred.
To change the location of a file's read-write pointer, you can use the file.seek (offset,from_what) function, which adds an offset value from a location. The From_what value is three, 0 means starting from the file header, 1 means starting at the current position, and 2 means starting at the end of the file. The default value is 0.
Reference: http://www.cnblogs.com/fnng/archive/2013/05/22/3091982.html
File read and write operations in "Python Learning Notes" pthon3.x