Python standard library: Built-in function open (file, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, closefd=t

Source: Internet
Author: User

This function opens a file and returns the file object. If the file cannot be opened, throw an exception OSError.

The argument file is a string representing the file name, or an array representing the file name. The file name can be the path relative to the current directory, or it can be an absolute path representation.

The parameter mode is the pattern that indicates the open file. The default value is ' r ', which means that the file is opened for reading using text.

' R ' means open file is read-only and cannot be written.

' W ' means that the open file is only written, and the file is emptied.

' X ' indicates an exclusive open file, which will fail if the file is already open.

' A ' means to open the file to write, not to empty the file, and append the file rear to the write.

' B ' indicates binary mode to open the file.

' t ' represents the text pattern, which is the default mode.

' + ' to open the file update (read or write).

The default mode is equivalent to ' RT '. For example ' W+b ' is to open the file to read and write, to empty the file, ' r+b ' open the file, but do not empty the file.

The parameter buffering is an optional parameter that represents the policy selection for the buffer. When set to 0 , indicates that no buffers are used, direct read and write, only valid in binary mode. When set to 1 , indicates how row buffers are used in text mode. When set to greater than 1 , indicates the set size of the buffer. If the parameter buffering is not given, using the default, the following policy is used to select:

1) for binary file mode, the fixed block memory buffer mode, the size of the memory block according to the allocation of the system device disk block to determine, if the size of the system disk block failure, the use of internal constant IO. the size defined by the default_buffer_size. On a typical operating system, the size of the block is 4096 or 8192 byte size.

2) The way a row of buffers is used for interactive text files (with isatty () as True). Other text files are used in the same way as the binary.

The parameter encoding is a file encoding that applies only to text files. If the encoding method is not known, the default is to use the encoding returned by the locale.getpreferredencoding () function.

The parameter errors is used to indicate how the encoding and decoding errors are handled. cannot be used in binary mode.

1) when indicated as ' strict ', the coding error throws an exception ValueError.

2) Ignore the errorwhen indicated as ' ignore '.

3) When specified as ' replace ', use a character for alternative mode, e.g. using '? ' To replace the error.

4) Other corresponding surrogateescape/xmlcharrefreplacs/backslashreplace are also available .

The parameter newline is used to control the end character of a line below the text pattern. Can be None,' ',\ n,\ r,\ r \ n and so on.

When in read mode, if the new line character is none Then work as a generic line break pattern, meaning when you encounter \n \r \r\n \n line break as text input. When set to null ', it is also common line break mode work, but does not convert to \n Enter what you like and keep it as it is. When set to a different character, the corresponding character is judged to be the line break and is entered as-is into the text.

When in output mode, if the new line character is None, all output text takes \ n as the newline character. If set to ' or \ n ' , no substitution action is made. If it is another character, it is added after the character \ n as the line break.

The parameter closefd is used when a file handle is passed in, and when the exit file is used, the file handle is not closed. If a file name is passed in, this argument is not valid and must be True .

The parameter opener is used to implement its own definition of open file mode. This method of use is more complex, such as open relative directory files, you can use the following example:

>>> Import OS

>>> dir_fd = Os.open (' Somedir ', OS. O_RDONLY)

>>> def opener (path, flags):

... return os.open (path, flags, DIR_FD=DIR_FD)

...

>>> with open (' Spamspam.txt ', ' W ', Opener=opener) as F:

... print (' This'll be written to Somedir/spamspam.txt ', file=f)

...

>>> Os.close (DIR_FD) # don ' t leak a file descriptor

Example:

#在C盘下面有一个文件abc. txt. #open () with open (R ' C:\\abc.txt ') as Demo: For Line in    Demo:        print (line)        

The resulting output is as follows:

The is test file.

The

3

4

8888888888888

Cai Junsheng qq:9073204 Shenzhen

Python standard library: Built-in function open (file, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, closefd=t

Related Article

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.