The "Python open function" section describes how to open a file with the open function. On top of this, this section goes on to explain how to read data from an opened file.
Python provides the following three functions, all of which can help us to read the data in the file:
This section first explains the use of the read function. The readline and readlines functions will be described in detail in subsequent chapters.
For files opened with the open function and in readable mode (including r, r +, rb, rb +), you can call the read function to read the contents of the file byte by byte (or character by character).
If the file is opened in text mode (non-binary mode), the read function reads character by character; conversely, if the file is opened in binary mode, the read function reads byte by byte.
The basic syntax of the read () function is as follows:
file.read ([size])
Among them, file represents the opened file object; size as an optional parameter is used to specify the maximum number of characters (bytes) that can be read at one time. If omitted, the default is to read everything at once.
For example, first create a text file named a.txt with the contents:
Python tutorial: www.freelearningpoints.com
Then write the following statement:
#Open specified file in utf-8 encoding format (output_13) f = open ("a.txt", encoding = "utf-8") #Output the read data print (f.read ()) #Close the file f.close ()
The output is:
Python tutorial: www.freelearningpoints.com
Note that when the operation file is finished, you must call the close function to manually close the opened file. This can avoid unnecessary errors in the program.
Of course, we can also specify the maximum number of characters (or bytes) that Python read function can read at a time by using the size parameter, for example:
#Open specified file in utf-8 encoding format (output_14) f = open ("a.txt", encoding = "utf-8") #Output the read data print (f.read (6)) #Close the file f.close ()
The output is:
Python
Obviously, the read function in this program reads only the first 6 characters of the a.txt file.
Again, size indicates the maximum number of characters (or bytes) that can be read at a time, so even if the size is set greater than the number of characters (bytes) stored in the file, the read function will not report an error, data in the file will be read.
In addition, for files opened in binary format, the read function reads the contents of the file byte by byte. Example:
#Open the specified file in binary form (output_15) f = open ("a.txt", 'rb +') #Output the read data print (f.read ()) #Close the file f.close ()
The output is:
b'Python tutorial: www.freelearningpoints.com'
As you can see, the output data is a bytes byte string. We can call the decode method to convert it into a string we know.
Another thing to note is that if you want to use the read function to successfully read the file content, in addition to strictly following the syntax of read function, it also requires that the open function must be readable by default (including r, r +, rb, rb + )open a file. For example, if the open mode of the above program is changed to w, the program will throw an io.UnsupportedOperation exception, indicating that the file does not have read permissions:
#Open the specified file in binary form (output_15) f = open ("a.txt", 'w') #Output the read data print (f.read ()) #Close the file f.close ()
The output is:
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
[ipython-input-20-fe9a310c1a98] in [module]
1 #Open the specified file in binary form
----> 2 f = open ("a.txt", 'w')
3 #Output the read data
4 print (f.read ())
5 #Close the file
PermissionError: [Errno 13] Permission denied: 'a.txt'
When using the read function, if the Python interpreter raises a UnicodeDecodeError exception, the reason is that the encoding format used by the object file does not match the encoding format used by the open function to open the file.
For example, if the encoding format of the target file is GBK encoding, and we use the open function and open the file in text mode, manually specify the encoding parameter as UTF-8. In this case, because the encoding format does not match, when we use the read function to read the data in the target file, the Python interpreter will prompt a UnicodeDecodeError exception.
To solve this problem, either change the encoding parameter value in the open function to the same encoding format as the object file, or regenerate the object file (that is, change the encoding format of the file to the same encoding parameter in the open function ).
In addition, there is another method:
1. First read the file in binary mode
2. Then call the decode method of bytes
3. Use the encoding format of the target file to convert the read byte string into a recognized string.
For example:
#Open the specified file in binary form, the file encoding format is utf-8 f = open ("a.txt", 'rb +') byt = f.read () print (byt) print ("\ nAfter conversion:") print (byt.decode ('utf-8')) #Close the file f.close ()
The output is:
b'Python tutorial: www.freelearningpoints.com'
After conversion:
Python tutorial: www.freelearningpoints.com'
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises