The complexity of file handling lies in the built-in approach-----python

Source: Internet
Author: User
Tags file handling

Initiate

File is where we store information, we often have to read, write, delete, and so on, in Python, we can use the functions and methods provided by Python to easily manipulate the file.

Functions and methods of file processing *************************************************

The open () function opens the file with the following syntax:

f = open (filename,mode,unicoding = "Utf-8")

FileName is the name of the file you want to manipulate, and if not the current path, specify the path. Mode is the pattern that opens the file, indicating how you want to manipulate the file, uniconding = "Utf-8" to declare the encoding format.

1. Mode

Mode

Describe

R

Open the file as read to read the file information.

W

Open a file in write mode to write information to the file.

A

Opens the file in Append mode, and the file pointer is automatically moved to the end of the file.

r+

The file can be read and written by opening the file in read/write mode.

w+

Remove the file contents, and then open the file as read-write.

A +

Open the file in read-write mode and move the file pointer to the end of the file.

B

Open the file in binary mode instead of in text mode. This mode is only valid for Windows or DOS, and Unix-like files are operated in binary mode.


The open () function returns a file object that can be read and written by the read () or write () function

2. File Object Methods

 

Method

Describe

F.close ()

Close the file, remember to open the file after opening () must remember to close it, otherwise it will occupy the system open file handle number.

F.flush ()

Refresh Output Cache

F.isatty ()

Returns true if the file is an interactive terminal, otherwise false.

F.read ([Count])

Reads the file, and if there is count, it reads count bytes.

F.readline ()

Read a line of information.

F.readlines ()

Reads all the lines, that is, the information that reads the entire file.

F.seek (Offset[,where])

Move the file pointer to the offset position relative to where. An offset of 0 indicates the beginning of the file, which is the default, 1 represents the current position, and 2 indicates the end of the file.

F.tell ()

Gets the file pointer position.

F.truncate ([size])

Intercepts the file so that the file size is sized.

F.write (String)

Writes string strings to a file.

Example

1.open

When opening a file with open, be sure to call the close () method of the file object to ensure that the file is finally closed.

File_object = open (' Thefile.txt ')

File_object.close ()

2. Read the file

Read text file

input = open (' Data ', ' R ')

#第二个参数默认为r

input = open (' Data ')

Read binary files

input = open (' Data ', ' RB ')

Read all content

File_object = open (' Thefile.txt ')

All_the_text = File_object.read ()

File_object.close ()

Read fixed byte

File_object = open (' Abinfile ', ' RB ')

While True:

Chunk = file_object.read (100)

If not chunk:

Break

Do_something_with (Chunk)

File_object.close ()

Read each line

List_of_all_the_lines = File_object.readlines ()

If the file is a text file, you can also traverse the file object directly to get each line:

For line in File_object:

Process Line

3. Writing files

Write a text file

Output = open (' Data ', ' W ')

Write a binary file

Output = open (' Data ', ' WB ')

Append Write file

Output = open (' Data ', ' w+ ')

Write Data

File_object = open (' Thefile.txt ', ' W ')

File_object.write (All_the_text)

File_object.close ()

Write Multiple lines

File_object.writelines (list_of_text_strings)

Note that calling Writelines writes multiple rows at a higher performance than using write one-time writes.

When processing log files, often encounter such a situation: The log file is huge, it is not possible to read the entire file into memory for processing, such as the need to process a 2GB log file on a machine with physical memory of 2GB, we may want to process only 200MB of content at a time.

In Python, the built-in File object directly provides a readlines (sizehint) function to accomplish such a thing. Take the following code as an example:

File = open (' Test.log ', ' r ') Sizehint = 209715200 # 200Mposition = 0lines = File.readlines (sizehint) while not File.tell () -position < 0:position = File.tell () lines = File.readlines (sizehint)

Each call to the ReadLines (Sizehint) function returns approximately 200MB of data, and the return is necessarily the complete row data, in most cases the number of bytes returned is slightly larger than the value specified by Sizehint (except for the last call to ReadLines ( Sizehint) function. Typically, Python automatically adjusts the value of the user-specified sizehint to an integer multiple of the internal cache size.

File is a special type in Python, which is used to manipulate external files in a Python program. In Python everything is an object, file is no exception, file has the method and properties of file. Let's look at how to create a file object:

File (name, mode, unicoding = "Utf-8")

The file () function is used to create a file object that has a name of open () and may be more image-built functions. Take a look at its parameters. Its parameters are passed as a string. Name is the name of the file.

Mode is open, and the optional value is R w a U, which represents read (default) write to add patterns that support various line breaks. If you open the file in W or a mode, it will be created automatically if the file does not exist. In addition, when you open an existing file in W mode, the contents of the original file will be emptied, because the operation of the first file is marked at the beginning of the file, and the writing operation will undoubtedly erase the original content. For historical reasons, newline characters in different systems have different patterns, such as in Unix is a \ n, and in Windows is ' \ r \ n ', in the U-mode to open the file, is to support all the newline mode, also say ' \ r ' \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ A tuple is used to store the newline characters used in this file. However, although there are multiple modes of line break, read the unified use \ n instead of Python. After the pattern character, you can also add a + B t these two identities, respectively, can be read and write to the file and in binary mode, text mode (the default) to open the file.

uniconding = "Utf-8" is the definition of the encoding format

The file object has its own properties and methods. First look at the properties of file.

Encoding #文件编码

Mode #打开模式

Name #文件名

Newlines #文件中用到的换行模式, is a tuple

Softspace #boolean型, typically 0, is said to be used for print

File read and Write methods:

F.read ([size]) #size为读取的长度, in bytes

F.readline ([size])

#读一行, if size is defined, it is possible to return only part of a row

F.readlines ([size])

#把文件每一行作为一个list的一个成员 and return to this list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.

F.write (str)

#把str写到文件中, write () does not add a newline character after Str

F.writelines (seq)

#把seq的内容全部写到文件中. This function simply writes faithfully and does not add anything behind each line.

Other methods of file:

F.close ()

#关闭文件. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates VALUEERROR

F.flush ()

#把缓冲区的内容写入硬盘

F.isatty ()

#文件是否是一个终端设备文件 (on UNIX systems)

F.tell ()

#返回文件操作标记的当前位置, starting with the origin of the file

Fnext ()

#返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.

F.seek (Offset[,whence])

#将文件打操作标记移到offset的位置. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.

F.truncate ([size])

#把文件裁成规定的大小, the default is to crop to the location of the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.

The complexity of file handling lies in the built-in approach-----python

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.