Python code Learning-Methods for file handling

Source: Internet
Author: User

File operation:
Os.mknod ("test.txt") Create an empty file
fp = open ("Test.txt", W) opens a file directly and creates a file if the file does not exist

About open mode:

W opens in     write mode, a     opens in append (starting with EOF, creates a new file if necessary) r+ open in read-write mode w+ open in read-write mode (see     W) A + opens     in read-write mode (see a) RB     Open WB in binary read mode     opens in binary write mode (see W) ab     opens in binary append mode (see a) rb+ opens    in binary read-write mode (see r+) wb+ opens in binary read-write mode    (see w+) Ab+
     opens in binary read/write mode (see A +)

The specific method

Fp.read ([size]) #size为读取的长度, in bytes,read it out as a stringFp.readline ([size]) #读一行, if size is defined, it is possible to return only a portion of a row fp.readlines ([size]) #把文件每一行作为一个list的一个成员, and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file. Fp.write (str) #把str写到文件中, write () does not add a newline character fp.writelines (seq) #把seq的内容全部写到文件中 after str (multi-line write-once) 。 This function simply writes faithfully and does not add anything behind each line. Fp.close () #关闭文件.  Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down.                                      If a file is closed and then manipulated, the Valueerrorfp.flush () #把缓冲区的内容写入硬盘fp is generated. Fileno ()                                         #返回一个长整型的 "File label" Fp.isatty () #文件是否是一个终端设备文件 (in Unix systems) Fp.tell () #返回文件操作标记的当前位置, the Origin Fp.next () #返回下一行 at the beginning of the file, and the file action marks the offset To the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal. Fp.seek (Offset[,whence]) #将文件打操作标记移到offset的位置. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as the origin.Count 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.The main purpose is to return to the beginning of the document seek (0)Fp.truncate ([size]) #把文件裁成规定的大小, the default is the location that is cropped to the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.no matter where you seek (30), truncate always intercepts the specified size from the beginning of the file.

Python code Learning-Methods for file handling

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.