Python Learning Summary 8: File mode and how-to summary

Source: Internet
Author: User

File is required before the file operation to ensure that the file exists, and the file open

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

1. The following patterns are common in file open:

W opens in write mode,
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode, open file will delete the original file contents

w+ Open in read-write mode (see W), open file will keep the original file content unchanged
A + opens in read/write mode (see a)
RB opens in binary read mode
WB 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 +)

2. Summary of common operations for files

  • Fp.read ([size]): size is the length of the read, in bytes
  • Fp.readline ([size]): Reads a line, if a size is defined, it is possible to return only part of a row
  • Fp.readlines ([size]): Takes each line of the file as a member of a list and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size indicates the total length of the read content, that is, it may be read only to a portion of the file.
  • Fp.write (str): Writes STR to a file, write () does not add a newline character after Str
  • Fp.writelines (seq): Writes the contents of the SEQ to the file (multiline write-once). This function simply writes faithfully and does not add anything behind each line.
  • Fp.close (): Closes the file.  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, it generates VALUEERROR
  • Fp.flush (): Writes the contents of the buffer to the hard disk
  • Fp.fileno (): Returns the "file label" of a long integer type
  • Fp.isatty (): Whether the file is a terminal device file (Unix system)
  • Fp.tell (): Returns the current position of the file action tag, starting with the origin of the file
  • Fp.next (): Returns the next line and shifts the file action marker 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]): Moves the file action marker to the offset position. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is not necessarily available, whence can be calculated from scratch for 0, and 1 means the origin is calculated at the current position. 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.
  • Fp.truncate ([size]): The file is cut to the specified size, the default is the location of the cut to the current file operation 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.

Python Learning Summary 8: File mode and how-to summary

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.