[Python] File processing

Source: Internet
Author: User

Python file Object

Python file object is used to manipulate the normal file handle, the use of file objects can be a series of file additions and deletions and other operations.

File built-in function open ()

The built-in function open () provides a common interface for initializing input/output operations. The open () built-in function returns a file object when the file is successfully opened, or a IOError exception is thrown. The basic syntax for open () is:

File_object = open (file_name, access_mode='r', Buffering=-1)

File_object

Returns the file object.

file_name

The string of the name of the file to open, either a relative path or an absolute path.

Access_mode (optional)

The mode in which the file opens.

File mode Operation
R Open in read mode
Ru/ua Open as read, with Universal line feed support
W Open in write mode
A Open in Append mode
r+ Open in read-write mode
w+ Open in read-write mode
A + Open in read-write mode
Rb Open in binary read mode
Wb Open in binary write mode
Ab Open in binary append mode
rb+ Open in binary read-write mode
wb+ Open in binary read-write mode
ab+ Open in binary read-write mode

Buffering (optional)

The buffer used by the file, 0 is not buffered, 1 means that only one row of data is buffered, and any other value greater than 1 uses the given value to represent the buffer size using the given value. The function is not provided or a given negative value represents the use of the system default buffering mechanism.

1

Files opened using the ' R ' or ' U ' mode must already exist. Files opened with ' W ' mode, if they exist, are the first
Empty first, and then (re) create. Files opened in ' A ' mode are prepared for append data, and all written data will be
Append to the end of the file. Even if you seek somewhere else. If the file does not exist, it will be created automatically, similar to ' W '
Mode to open the file.

Indicates that the file open mode string can also contain the character "B", but it cannot be done as the first character appears. Doing this
The purpose is to strictly meet the requirements of ANSI c3.159-1989 (i.e. ANSI C); In fact it doesn't have any effect, all
POSIX compliant systems, including Linux, will ignore "B" (other systems may differentiate between text files and binaries if
You have to deal with a binary file and hope that your program can be ported to other non-UNIX environments, plus "B" would be nice
IDEA).


You can find a detailed list of file access patterns in table 9.1, including the use of ' B '-if you choose to use
its words. If there is no given access_mode, it will automatically take the default value of ' R '.

Here are some examples of open files:
fp = open ('/ETC/MOTD ') # opens in read mode
fp = open (' Test ', ' W ') # opens in write mode
fp = open (' Data ', ' r+ ') # opens in read/write mode
FP = open (R ' C:\io.sys ', ' RB ') # opens in binary read mode

Universal line break Support (UNS)

In the next core note, we'll show you how to use some of the properties of the OS module to help you access the text under different platforms.
Different platforms to indicate that the end of the line is different, for example \ n, \ r, or \ r \ n. So, the explanation for Python

To handle such tasks, especially when importing modules. Don't you want Python to handle it the same way
All the files?

This is the key to UNS, and as a result of PEP 278, Python 2.3 introduces UNS. When you use the ' U ' flag
When the file is opened, all line separators (or line terminators, whatever it is) are entered via Python (example
If read* ()) is returned, it will be replaced with a newline character newline (\ n). (' RU ' mode also supports ' RB ' option). This feature
Files that contain different types of line terminators are also supported. The Newlines property of the file object records the line knot of the file it had "seen"
Shing.
If the file has just been opened and the program has not encountered a line terminator, then the newlines of the file is none. The first line is read
When taken, it is set to the first line of the Terminator. If another type of line terminator is encountered, the newlines of the file becomes a
Tuples that contain each format. Note UNS is only used to read text files. There is no corresponding method to process the output of the file.
When compiling Python, UNS is turned on by default. If you don't need this feature, run the Configure script
You can use the--without-universal-newlines switch to turn it off. If you want to handle the line terminator yourself,
Review the core notes and use the relevant properties of the OS module.

[Python] File processing

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.