17 file opening mode and file object Method

Source: Internet
Author: User

File opening Mode

Open Mode Perform operations
'R' Open a file in read-only mode (default)
'W' Opening the file as a write will overwrite the existing file
'X' If the file already exists, opening in this mode will cause an exception.
'A' Open in write mode. If the file exists, append it to the end.
'B' Open a file in binary mode
'T' Open in text mode (default)
'+' Read/write mode (can be added to other modes)
'U' Support for common line breaks



File object Method

File object Method Perform operations
F. Close () Close file
F. Read ([size =-1]) Read size characters from the file. When no size is specified or a negative value is given, read all the remaining characters and return them as strings.
F. Readline ([size =-1]) Read from the file and return a row (including the row Terminator). If the size is defined, the return size is a string of characters.
F. Write (STR) Write string STR to file
F. writelines (SEQ) Writing string sequence seq to a file. seq should be an iteratable object that returns a string.
F. Seek (offset, from) Move the file pointer in the file, and offset the offset byte from (0 indicates the start position of the file, 1 indicates the current position, and 2 indicates the end of the file ).
F. Tell () Returns the current position in the file.
F. truncate ([size = file. Tell ()]) Truncates a file to the size byte. By default, it is truncated to the current position of the file pointer.

17 file opening mode and file object 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.