Python file operations

Source: Internet
Author: User
Tags readline

Procedure for file operation

    1. Open the file, get the file handle and assign a value to a variable
      1. F=open ("Path", "R", encoding= "Utf-8")
    2. Manipulating a file with a handle
      1. F.read,f.write F.readline () a row reads, all in memory, F.readline (), Freadlines () One-time read, into a dictionary, large file read efficiency, may cause crashes.
        1. For i in F: Read-by-line and freed from memory, read faster, read large files without causing PC crashes
    3. Close File
      1. F.close ()

        File open once, can only read once, open file remember close, related method, F.seek () move handle position, in character count,

      2. F.tell () returns the handle position, typically using the with open ("A.txt", "W", enconding= "Utf-8") as F: This will automatically shut down after the use is complete, and will not wait until the GC is working to close the file.

        The mode of opening the file is:

        • R, read-only mode (default).
        • W, write-only mode. "unreadable; not exist; create; delete content;"
        • A, append mode. "Readable; not exist" create; "only append content;"

        "+" means you can read and write a file at the same time

        • r+, can read and write files. "readable; writable; can be added"
        • w+, write and read
        • A +, with a

        "U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading

        • RU
        • R+u

        "B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)

        • Rb
        • Wb
        • Ab

Python 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.