Python file read/write

Source: Internet
Author: User

Python file reading and writing is determined by the parameters of the open () function to read and write

Open (file, mode= ' R ', Buffering=none, Encoding=none, Errors=none, Newline=none, Closefd=true)
The Open function defaults to r mode as read-only mode, the Open function is shared with R r+ W w+ A + six modes, the following is the explanation of 6 modes

Functions for reading and writing files

F=open ('Book.txt', encoding='Utf-8') F.read ()#the method reads in from the beginning of the file, and if it does not pass in count, it tries to read as much more content as possible, most likely until the end of the file. F.readline ()#read a row of a fileF.readlines (Sizeint)#reads all the rows and returns the list, and, given sizeint>0, sets how many bytes are read at a time, in order to alleviate the read pressure. F.write ()#writes a string to the file, returning the length of the written character. F.writelines ()#writes a list of sequence strings to the file and adds a newline character to each line if a line break is required. F.seek ()#set the current location of the fileF.tell ()#returns the current location of the fileF.truncate (f)#Empty FileF.flush ()#writes the contents of a buffer to a fileF.close ()#Close File

Python file read/write

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.