Use of the Open function in Python

Source: Internet
Author: User
Tags function prototype

First, the function prototype of open ()
Open (file, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, Closefd=true)
From the official documents we can see the open function has a lot of parameters, we commonly used are file,mode and encoding, for other parameters, usually not commonly used, but also a brief introduction.
The buffering values are 0,1, >1 three, 0 for buffer off (binary mode only), 1 for line buffer (only for text mode), and >1 for the initialized buffer size;
Encoding indicates what encoding is used for the returned data, generally using UTF8 or GBK;
Errors value generally have strict,ignore, when take strict, when the character encoding problems, will error, when taking ignore, coding problems, the program will be ignored, continue to execute the following program.
NewLine can take a value of None, \ n, \ r, ', ' \ r \ n ', to differentiate the newline character, but this parameter is only valid for text mode;
The value of the CLOSEFD, is related to the incoming file parameters, by default, True, the file parameter passed to the file name, the value is false, file can only be a document descriptor, what is a file descriptor, is a non-negative integer, in the Unix kernel system, When you open a file, a file descriptor is returned.
Second, file () and open ()
Both can open the file, the operation of the file, but also have similar usage and parameters, but, in my opinion, the two file opening methods have an essential difference, file for the document class, with file () to open the files, equivalent to this is in the construction of the file class, and open () opened the file, is done using Python's built-in functions.
Three, the basic value of the parameter mode

R, W, A is the basic mode of open file, corresponding to read-only, write-only, append mode;
B, T, +, u these four characters, with the above file open mode combination, binary mode, text mode, read and write mode, universal line break, according to the actual situation combination of use,

Iv. Common mode value combinations
1, R or RT default mode, text mode read
2, RB binary files
3, w or wt text mode write, before opening the file storage is emptied
4, WB binary write, file storage is also emptied
5, a append mode, can only be written at the end of the file
6, A + read-write mode, write can only be written at the end of the file
7, w+ can read and write, and A + is the difference is to clear the contents of the file
8, r+ can read and write, and A + difference is can write to the file anywhere

Five, the difference between the several models
To test the differences between the different modes, we use a small piece of code to test the visual difference in writing the file.

?
1234567 test = [ "test1\n", "test2\n", "test3\n" ]  f = open( "b.txt", "a+")  try:    for s in test:      f.write( s )  finally:    f.close()

(1) The difference between A + and w+ modes

(2) A + and r+ mode

Before writing the file, we add a seek (6) in the above code to locate the write file to the location.

Note: This file must exist when the file is opened in r+ mode, otherwise it will be an error, and the ' R ' mode is the same
Six, the trouble of line break
When you use binary mode to write a string with a newline character to the TXT file, the data store is correct, but when you open it with the Notepad program of the Windows platform, you see a little black block of the newline character, but in text mode, there is no such problem.
Here, different platforms are involved because of the coding problem, and the line breaks are identified differently. The UNIX or Linux system recognizes \ nthe identity of the newline character, but the encoding of the Windows platform ignores the \ n.
But Python itself with the conversion function, in the text mode, you will not see due to different platforms resulting in different line effects, but, in the binary mode, Python will not be converted, what, write in what, at this time the line break, and then open with text mode, The ' \ n ' line break is not recognized under Windows.

Use of the Open function in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.