Python core programming (Chapter Nineth)-File and input/output

Source: Internet
Author: User

File built-in function: the open () function provides a common interface for initializing input/output operations open () basic syntax:File_object = open (filename,access_mode= ' R ', buffering=-1) filename is a string that contains the name of the file to be opened (can be a relative or absolute path) Access_mode is also a string , usually ' R ', ' W ', ' A ' for read, write, and append, the file opened using ' R ' mode must already exist. ' W ' mode open files are emptied prior to re- Factory function file ():As with open (), you can replace the built-in methods with each other: file methods are divided into four categories: input, output, intra-file move, and miscellaneous operations Input:1, read (): Read bytes into a string, there is an optional parameter size, the default is-1, if 1 or a complex number then the file will be read to the end of the file. 2, ReadLine (): Reads a line of the file, including the line terminator. The same as read () also has an optional parameter size. 3, ReadLines (): Read all (remaining) and then return them as a list of strings, it has an optional parameter sizhint represents the maximum word size returned. Output:1. Write (): writes a string containing text data or binary data blocks to a file. 2, Writelines (): For the list operation, accept a list of strings as parameters, write them to the file, the line terminator is not automatically added, if necessary, you must call the Writelines method before the end of each line to add a terminator. When reading rows from a file using read () or readlines (), Python does not delete the line terminator moving within a file:The Seek () method can move a file pointer to a different location in a file Syntax:
Fileobject.seek (offset[, whence])
Parameters: The following is the detailed parameter: offset: The location of the read/write pointer to the file. Whence: This is optional, the default is 0, which means absolute file positioning, the other value is 1, which means that the current position and 2 means seek relative to the end of the file. file iterations:For Eachline in F: file Close:F.close () built-in methods: file Built-in properties:

Python core programming (Chapter Nineth)-File and input/output

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.