Python file read and write operations

Source: Internet
Author: User

The following steps are generally experienced when manipulating files:

  • Open File
  • Manipulating files

1. Open the file:

File handle = open ('/path/to/file_name ', ' pattern ')

Pattern

  • R: Read-only mode, which defaults to read-only mode when opening a file, and the action pointer at the beginning of the file.
  • W: Write-only mode, the file does not exist, it is created, the original content is emptied, and the action pointer is at the beginning of the file.
  • X: Write-only mode, the file does not exist, it is created, there is a report; The action pointer is at the beginning of the file.
  • A: Append, write only, the file does not exist then create, exist in the original file append content, the action pointer to the end of the file.

+ indicates both read and write files:

  • r+: Read and Write
  • w+: Write Read
  • x+: Write Read
  • A +: Additional Reading

B means to operate in byte mode:

  • RB: Open in bytes, read and write
  • WB: Open in bytes, write read
  • XB: Opened in bytes, read-write
  • AB: Open in bytes, append read

  NOTICE: When opened in B, the data read is byte type, and the data written is also of type Byte.

Through the With management context:

With open ('/path/to/file_name ', ' pattern ') as file_name:

Pass

With support for managing multiple contexts at python2.7 and beyond

With open ('/path/to/file_name1 ', ' pattern ') as File_name1,open ('/path/to/file_name2 ', ' pattern ') as file_name2:

Pass

Python file read and write operations

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.