Python Learning Notes--open mode for files and object methods for files

Source: Internet
Author: User

1 #in Python, if you want to manipulate a file, you must first get the object of the file. 2 #Example One:3 Print("-------------------------------------------------------------------")4File = Open ("Test.txt","WT")#Here we use the open () function to get the object. 5data ="This is a test file!"6File.write (data)#writing data to a file7File.close ()#Remember to release the file object8 9 #then we came to the directory where the program was located and found that a file named Test.txt was created hereTen #open it and we can see that some data has been written into it.  One Print("-------------------------------------------------------------------") A  - #Here are some of the file's open patterns and Object methods - """ the Open mode of file: - "R": Open file in read-only mode (default) - "W": Open file as written, if file does not exist, create a file if file exists, overwrite existing file - "x": If the file already exists, open with this mode will throw an exception + "A": Open in write mode, append write at end If file exists - "B": Open the file in binary mode + "T": Open the file as text A "+": Read/write mode (can be added to other modes for use) at "U": Universal line break support -  - object method of the file: - file.close () #关闭文件 - F.read ([size=-1]) #从文件读取size个字符, when a size is not given or a negative value is given, all the remaining characters are read and then returned as a string - F.readline ([size=-1]) #从文件中读取并返回一行 (including line terminator), returns a size character if there is a size defined in f.write (str) #将字符串str写入文件 - f.writelines (SPE) #向文件写入字符串序列sep, Sep should be an iterative object that returns a string to F.seek (Offset,from) #在文件中移动文件指针, from (0 for the starting position of the file, 1 for the current position, 2 for the end of the file), OFFSE (the value is the offset of the number of bytes) + F.tell () #返回当前在文件中的位置 - f.truncate ([Size=file.tell ()]) #截取文件到size个字节, the default is to intercept the current position of the file pointer the  * """

Python Learning Notes--open mode for files and object methods for files

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.