Python3.5 Learning notes-file operations

Source: Internet
Author: User

in the in Python, the action file object is created using the open function, and the following table lists the functions that are commonly used for file manipulation :

Serial Number
Method and Description

1.file.close ()

Close the file. The file can no longer read and write after closing.

2.file.flush ()

refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write.

3.file.fileno ()

returns a file descriptor for an integer type (File descriptor FD integer ), can be used in such OS module's Read methods, and so on some underlying operations.

4.file.isatty ()

if the file is connected to an end device, return True, 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 row, including The "\ n" character.

8.file.readlines ([Sizehint])

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 more than Sizhint larger , because the buffer needs to be populated.

9.file.seek (offset[, whence])

Set the current location of the file

10.file.tell ()

Returns the current location of the file.

11.file.truncate ([size])

intercepts the file, the truncated byte is specified by size, and the default is the current file location.

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.

Example diagram

Python3.5 Learning notes-file operations

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.