Python basic--file (file) method

Source: Internet
Author: User

The file object is created using the Open function, and the following table lists the functions commonly used by file objects:

Serial Number Method and Description
1

File.close () closes the file. The file can no longer read and write after closing.

2

File.flush () Refreshes the internal buffer of the file, directly writes the data of the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written.

3

The File.fileno () returns a file descriptor for an integer type (filename descriptor FD integer), which can be used in some underlying operations, such as the Read method of the OS module.

4

File.isatty () returns True if the file is connected to an end device, otherwise False is returned.

5

File.next () returns the next line of the file.

6

File.read ([size]) reads the specified number of bytes from the file, if none is given or is negative.

7

File.readline ([size]) reads the entire line, including the "\ n" character.

8

File.readlines ([sizeint]) reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizeint because the buffer needs to be populated.

9

File.seek (offset[, whence]) sets the current location of the file.

10

File.tell () returns the current location of the file.

11

File.truncate ([size]) truncates from the beginning of the first line of the file, truncates the file to size characters, no size indicates truncation from the current position, and all subsequent characters after truncation are deleted, where the line break under the Widnows system represents a 2-character size.

12

File.write (str) writes a string to the file without a return value.

13

File.writelines (Sequence) writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.

Python basic--file (file) method

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.