Python built-in functions (47) -- open, python built-in 47 open

Source: Internet
Author: User

Python built-in functions (47) -- open, python built-in 47 open

English document:

open(File,Mode = 'R',Buffering =-1,Encoding = None,Errors = None,Newline = None,Closefd = True,Opener = None)

OpenFileAnd return a corresponding file object. If the file cannot be opened,OSErrorIs raised.

FileIs either a string or bytes object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unlessClosefdIs setFalse.)

ModeIs an optional string that specifies the mode in which the file is opened. It defaults'r'Which means open for reading in text mode. Other common values are'w'For writing (truncating the file if it already exists ),'x'For exclusive creation and'a'For appending (which onSomeUnix systems, means thatAllWrites append to the end of the file regardless of the current seek position). In text mode, ifEncodingIs not specified the encoding used is platform dependent:locale.getpreferredencoding(False)Is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leaveEncodingUnspecified.) The available modes are:

Character Meaning
'r' Open for reading (default)
'w' Open for writing, truncating the file first
'x' Open for exclusive creation, failing if the file already exists
'a' Open for writing, appending to the end of the file if it exists
'b' Binary mode
't' Text mode (default)
'+' Open a disk file for updating (reading and writing)
'U' Universal newlines mode (deprecated)

The default mode is'r'(Open for reading text, synonym'rt'). For binary read-write access, the mode'w+b'Opens and truncates the file to 0 bytes.'r+b'Opens the file without truncation.

As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including'b'InModeArgument) return contentsbytesObjects without any decoding. In text mode (the default, or when't'Is already ded inModeArgument), the contents of the file are returnedstr, The bytes having been first decoded using a platform-dependent encoding or using the specifiedEncodingIf given.

Note

Python doesn't depend on the underlying operating system's notion of text files; all the processing is done by Python itself, and is therefore platform-independent.

BufferingIs an optional integer used to set the buffering policy. pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode ), and an integer> 1 to indicate the size in bytes of a fixed-size chunk buffer. when noBufferingArgument is given, the default buffering policy works as follows:

  • Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device's "block size" and falling back onio.DEFAULT_BUFFER_SIZE. On Distributed systems, the buffer will typically be 4096 or 8192 bytes long.
  • "Interactive" text files (files for whichisatty()ReturnsTrue) Use line buffering. Other text files use the policy described above for binary files.

EncodingIs the name of the encoding used to decode or encode the file. This shoshould only be used in text mode. The default encoding is platform dependent (whateverlocale.getpreferredencoding()Returns), but any text encoding supported by Python can be used. SeecodecsModule for the list of supported encodings.

ErrorsIs an optional string that specifies how encoding and decoding errors are to be handled-this cannot be used in binary mode. A variety of standard error handlers are available (listed under Error Handlers), though any error handling name that has been registeredcodecs.register_error()Is also valid. The standard names include:

  • 'strict'To raiseValueErrorException if there is an encoding error. The default valueNoneHas the same effect.
  • 'ignore'Ignores errors. Note that ignoring encoding errors can lead to data loss.
  • 'replace'Causes a replacement marker (such'?') To be inserted where there is malformed data.
  • 'surrogateescape'Will represent any incorrect bytes as code points in the Unicode Private Use Area ranging from U + DC80 to U + DCFF. These private code points will then be turned back into the same bytes whensurrogateescapeError handler is used when writing data. This is useful for processing files in an unknown encoding.
  • 'xmlcharrefreplace'Is only supported when writing to a file. Characters not supported by the encoding are replaced with the appropriate XML character reference&#nnn;.
  • 'backslashreplace'Replaces malformed data by Python's backslashed escape sequences.
  • 'namereplace'(Also only supported when writing) replaces unsupported characters\N{...}Escape sequences.

NewlineControls how universal newlines mode works (it only applies to text mode). It can beNone,'','\n','\r', And'\r\n'. It works as follows:

  • When reading input from the stream, ifNewlineIsNone, Universal newlines mode is enabled. Lines in the input can end in'\n','\r', Or'\r\n', And these are translated'\n'Before being returned to the caller. If it is'', Universal newlines mode is enabled, but line endings are returned to the caller untranslated. if it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
  • When writing output to the stream, ifNewlineIsNone, Any'\n'Characters written are translated to the system default line separator,os.linesep. IfNewlineIs''Or'\n', No translation takes place. IfNewlineIs any of the other legal values, any'\n'Characters written are translated to the given string.

IfClosefdIsFalseAnd a file descriptor rather than a filename was given, the underlying file descriptor will be kept open when the file is closed. If a filename is givenClosefdMust beTrue(The default) otherwise an error will be raised.

A custom opener can be used by passing a callableOpener. The underlying file descriptor for the file object is then obtained by callingOpenerWith (File,Flags).OpenerMust return an open file descriptor (passingos.openAsOpenerResults in functionality similar to passingNone).

 

Note:

1. The function opens a file, returns a file read/write object, and can perform corresponding read/write operations on the file.

2. The file parameter indicates the relative path (current working directory) or absolute path of the file to be opened. If the input path does not exist, an error is returned. Or input the file handle.

>>> A = open('test.txt ') # Relative Path >>> a <_ io. textIOWrapper name='test.txt 'mode = 'R' encoding = 'cp936' >>>. close () >>> a = open (r 'd: \ Python \ Python35-32 \ test.txt ') # absolute path >>> a <_ io. textIOWrapper name = 'd: \ Python \ Python35-32 \ test.txt 'mode = 'R' encoding = 'cp936'>

3. The mode parameter indicates the mode in which the file is opened. Common Open modes include the following. You can combine them according to the actual call conditions.

'R': open in read-only mode (default mode) (the file must exist)
'W': open in write-only mode. If the object exists, the object is automatically cleared and re-created. If the object does not exist, the object is created. To use this mode, you must ensure that the directory where the file is located exists. The file may not exist. The read * () method cannot be used in this mode.

'A': In append mode. If the object exists, it is appended to the end of the object. If the object does not exist, a new object is created. The read * () method cannot be used in this mode.

  

The following four modes must be used in combination with the preceding modes.
'B': open in binary mode

'T': open in text mode (default mode)
'+': Enabled in read/write mode.
'U': open in common line break mode

Common mode combinations


'R' or 'rt ': default mode, text read mode
'W' or 'wt ': open in text writing mode (files are cleared before being opened)
'Rb': enabled in binary read mode
'AB': enabled in binary append Mode
'Wb ': open in binary write mode (files are cleared before opening)
'R + ': it is opened in text read/write mode and can be written to any location of the file. The default write pointer starts at the beginning of the file, so it overwrites the file.
'W + ': open in text read/write mode (files are cleared before being opened ). You can use read *()
'A + ': open in text read/write mode (write can only be written at the end of the file ). You can use read *()
'Rb + ': enabled in binary read/write mode
'Wb + ': open in binary read/write mode (files are cleared before being opened)
'AB +': enabled in binary read/write mode

 

# T is text read/write, B is binary read/write >>> a = open('test.txt ', 'rt') >>>. read () 'some text' >>> a = open('test.txt ', 'rb') >>>. read () B 'some text' # r is read-only and cannot be written. w is write-only and cannot be read >>> a = open('test.txt ', 'rt') >>>. write ('more text') Traceback (most recent call last): File "<pyshell #67>", line 1, in <module>. write ('more text') io. unsupportedOperation: write >>> a = open('test.txt ', 'wt') >>>. read () Traceback (most recent call last): File "<pyshell #69>", line 1, in <module>. read () io. unsupportedOperation: not readable # Other examples

4. buffering indicates the buffer policy used to read files.

0: indicates that buffer is disabled (only applicable to binary mode)
1: line buffer (applicable only to text mode)
> 1: indicates the initial buffer size.

5. The encoding parameter indicates the file encoding format used to read and write files.

Assume that the test.txt file stores the text in UTF-8 encoding:

>>> A = open('test.txt ', 'rt') # The encoding is incorrectly specified. An error may be returned.>. read () Traceback (most recent call last): File "<pyshell #87>", line 1, in <module>. read () UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence >>>> a = open('test.txt ', 'rt ', encoding = 'utf-8')>. read () 'I am 1st lines of text, I will be displayed on the screen \ n I am 2nd lines of text, I will be displayed on the screen \ n I am 3rd lines of text, I will be displayed on the screen '>>>

6. The errors parameter indicates the error severity when an error occurs during file read/write.

Common Errors include:

  • 'strict'Strict level. If an error is reported during character encoding, an exception is thrown, which is also the default level. If the errors parameter value is set to None, this level is used.
  • 'ignore'Ignore level, character encoding error, ignore.
  • 'replace'Replacement level. If the character encoding is incorrect, replace it ?.
>>> A = open('test.txt ', 'rt', encoding = 'utf-8') >>>. read () 'I am 1st lines of text, I will be displayed on the screen \ n I am 2nd lines of text, I will be displayed on the screen \ n I am 3rd lines of text, I will be displayed on the screen '>>> a = open('test.txt', 'rt ') >>>. read () Traceback (most recent call last): File "<pyshell #91>", line 1, in <module>. read () UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence >>>> a = open('test.txt ', 'rt ', errors = 'ignore')>. read () "Why? why? '> a = open('test.txt ', 'rt ', errors = 'replace')>. read () "Why ?" why? too many'

7. newline is used to differentiate line breaks (valid only for text mode. The values that can be obtained include None, '\ n',' \ R', '', '\ r \ n ')

>>> A = open('test.txt ', 'rt', encoding = 'utf-8', newline = '\ R') >>>. readline () 'I am 1st lines of text, I will be displayed on the screen \ R'> a = open('test.txt', 'rt ', encoding = 'utf-8 ', newline = '\ n') >>>. readline () 'I am 1st lines of text and I will be displayed on screen \ r \ N'

8. closefd indicates the type of the input file parameter (the default value is True). The input file path must be True, and the input file handle must be False.

>>> a = open('test.txt','rt',encoding = 'utf-8',newline = '\n',closefd = False)Traceback (most recent call last):  File "<pyshell#115>", line 1, in <module>    a = open('test.txt','rt',encoding = 'utf-8',newline = '\n',closefd = False)ValueError: Cannot use closefd=False with file name>>> a = open('test.txt','rt',encoding = 'utf-8',newline = '\n',closefd = True)

 

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.