Python advanced 7 _ file operations (1)

Source: Internet
Author: User

1. Basic file operations 1. Basic File Operations Overview

In python, a file object is a built-in file instance. The built-in function open creates and returns a file object. Function prototype fp = open(‑test.txt "," r "). The only difference between reading and writing is the second parameter problem, that is, open mode.

Let's take a simple example:



For the open mode, you can refer to the following:

R opened in Read mode

W open in write mode,

A is opened in append mode (starting from EOF and creating a file if necessary)

R + Enabled in read/write mode

W + is enabled in read/write mode (see w)

A + is enabled in read/write mode (see)

Rb is enabled in binary read mode.

Wb is enabled in binary write mode (see w)

AB is enabled in binary append mode (see)

Rb + is enabled in binary read/write mode (see r +)

Wb + is enabled in binary read/write mode (see w +)

AB + is enabled in binary read/write mode (see a +)

There are many built-in functions for file object fp, which are common:

OS. mknod ("test.txt ")

# Create an empty file

Fp. read ([size])

# Size indicates the read length, in bytes.

Fp. readline ([size])

# Read a row. If the size is defined, only one part of the row may be returned.

Fp. readlines ([size])

# Use each row of the file as a member of a list and return this list. In fact, it is implemented by calling readline () cyclically. If the size parameter is provided, size indicates the total length of the read content, that is, it may be read only to a part of the file.

Fp. write (str)

# Write str to a file. write () does not add a linefeed after str.

Fp. writelines (seq)

# Write All seq content to the file (multiple rows are written at one time ). This function is only faithfully written without adding anything to the end of each line.

Fp. close ()

# Close the file. Python will automatically close the file after a file is not used, but this function is not guaranteed, it is best to develop your own habit of closing. If a file is closed and operated on it, ValueError is generated.

Fp. flush ()

# Write the buffer content to the hard disk

Fp. fileno ()

# Return a long integer "file tag"

Fp. isatty ()

# Whether the file is a terminal device file (in unix)

Fp. tell ()

# Return the current position of the file operation mark, starting with the file

Fp. next ()

# Return the next row and move the operation mark of the file to the next row. Use a file... When a statement such as in file is called, the next () function is called to implement traversal.

Fp. seek (offset [, whence])

# Move the file to the offset position by marking the operation. 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, it is not necessary. If the whence parameter is 0, it indicates that the calculation starts from the beginning, and 1 indicates that the calculation is based on the current position. 2 indicates that the origin is the end of the file. Note that if the file is opened in a or a + mode, the Operation mark of the file is automatically returned to the end of the file each time the file is written.

Fp. truncate ([size])

# Crop the file to a specified size. The default value is to crop it to the position marked by the current file operation. If the size is larger than the file size, the file may not be changed depending on the system, or 0 may be used to fill the file with the corresponding size, it may also be added with random content.

2. Read and Write files

Let's take a look at a simple example of reading a file:


In the above operations, the size of the test.txt file is 41kb, but the gap has been generated. That is to say, when your memory permits, we recommend that you read the file at one time. The speed is the fastest. Of course, we can also easily use readlines for reading. This row-by-row iterative reading method can save a lot of memory resources.

Let's take a look at an example of writing a file:



When we run the program, we find that there is only writeTo2 content in test.txt. Note that when you open a file in the "w" or "wb" mode, the file content will be erased, let's try:




After running the program, we found that test.txt was empty! If we want to add new data to the original data, we should use "a", "AB". Remember to take a look at the example:


 





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.