Python open () function file processing

Source: Internet
Author: User

function: Open ()

1: Function: Open a file

2: Syntax:

Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true]])


3: Parameter Description:
File: The filename to open, plus the path (unless it is in the current directory). Unique Mandatory Parameters
Mode: File Open pattern
Buffering: Set buffer (value is 0,1,>1)
Encoding: The encoding of the returned data (typically UTF8 or GBK)
Errors: Error level (typically Strict,ignore)
NewLine: Used to differentiate between newline characters (only valid for text mode, can take a value of none, ' \ n ', ' \ R ', ' \ r \ n ')
CLOSEFD: The file parameter type passed in (default is True)

Commonly used are the three parameters of File,mode and encoding

4: Parameter Detailed description:
4.1.mode: File open mode. There are several modes
' R ': Open in read-only mode (default mode) (Must guarantee file presence )
' W ': Open in write-only mode. If the file exists, the file is automatically emptied and recreated, and if the file does not exist, a new file is created. This mode must be used to ensure that the file exists in the directory and that the file can not exist. The read* () method cannot be used in this mode

' A ': Opens in Append mode. If the file exists, it is appended to the end of the file, and if the file does not exist, the file is created. This mode cannot use the read* () method.

The following four modes are used in combination with the above pattern
' B ': Open in binary mode
' t ': Open in text mode (default mode)
' + ': Open in read/write mode
' U ': Opens in Universal line break mode

Common mode combinations
' R ' or ' RT ': default mode, text read mode
' W ' or ' wt ': Open in Text write mode (the file will be emptied before opening )
' RB ': Open in binary read mode
' AB ': Open in binary append mode
' WB ': Open in binary write mode (the file will be emptied before opening)
' r+ ': Open in text read/write mode, can write to any location of the file, the default write pointer starts at the beginning of the file, so the file will be overwrite
' w+ ': Open in text read/write mode (the file will be emptied before opening). 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+ ': Open in binary read/write mode
' wb+ ': Open in binary read/write mode (the file will be emptied before opening)

' ab+ ': Open in binary read/write mode


4.2.buffering: Set Buffer
0: On behalf of buffer off (only for binary mode)
1: Line buffer (for text mode only)
>1: Represents the initialized buffer size

4.3.errors: Error level
Strict: Error when character encoding problem occurs
Ignore: Character encoding occurs when the program ignores the problem and continues with the following program

4.4.CLOSEFD:
True: The file parameter passed in is the filename
False: The passed-in file parameter can only be a filename descriptor
Ps: A file descriptor is a non-negative integer that, in the Unix kernel system, opens a file and returns a file descriptor.

Note: When opening a file with open, always remember to close the file object


5: Example:
There is a file Test.txt, the file content is as follows
‘‘‘
Hello Good boy

How is it?

I am fine.
‘‘‘

5.1: Open in the default way
>>> f = open (' test.txt ') >>> print f.read () Hello good boyhow is you? I am fine.>>> f.close ()

5.2: Open in write-only mode
>>> f = open (' Test.txt ', ' W ') >>> F.read () Traceback (most recent call last):  File "<pyshell#9 > ", Line 1, in <module>    f.read () ioerror:file not open for reading>>> f.write (' Good bye!\n ') >> ;> f.close () >>> f = open (' test.txt ') >>> print f.read () good bye!>>> f.close ()

5.3: Open in Append mode
>>> f = open (' Test.txt ', ' a ') >>> f.write (' See You tomorrow!\n ') >>> f.close () >>> f = Open (' Test.txt ', ' R ') >>> print f.read () good bye!see you tomorrow!>>> f.close ()

5.4: Determine if the file is closed
>>> Try:all_the_text = F.read () finally:f.close () >>> f.read () Traceback (most recent call last):  File "<pyshell#60>", line 1, in <module>    f.read () valueerror:i/o operation on closed file
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.



6: Others:
File () and open ()
Same point: Both are able to open files, manipulate files, and use similar parameters

Different points:
File to open files, equivalent to this is in the construction file class

Open opens the file, using Python's built-in function to manipulate


Python open () function file processing

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.