Python path-file operations

Source: Internet
Author: User

Get file handle: File = open (filepath,mode,encoding) opens files under FilePath path in mode mode, and coding encoding (where mode can be r,w,a, read and write r+ (common), write w+ (w+    Open File and W mode open the file will overwrite the original file content, so not commonly used), append read a+;rb,wb,ab) Read content: Read () >>> read all the contents of the file Note: Read can add parameters to specify the number of reads ReadLine () >>> reads a line of content into the file: write ("content") >>> write to file close file handle: Close () Gets the location of the file cursor: Tell () Move cursor: Seek (index) >>> move file cursor to index position empty buffer: flush () >>> write data in buffer to file truncated text: Truncate (start) >> > Truncate (delete) article content from start position with statement:
With
open (' Log1 ') as Obj1,open (' log2 ') as Obj2:...........................pass ..................................
manipulating files through the WITH statement the Python interpreter automatically closes files after the With statement ends
Note:
    1. Writing a string in binary to a file requires encoding a string such as Str.encode ("Utf-8")
    2. Using a For loop file, each time the loop reads a row in the file

Python path-file operations

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.