PYTHON file Operations

Source: Internet
Author: User

Procedure for file operation

    1. Open the file, get the file handle and assign a value to a variable
    2. Manipulating a file with a handle
    3. Close File

The existing files are as follows

Beautiful is better than ugly.   Beauty is better than ugliness explicit is better than implicit.      Clarity is better than obscure simple is better than complex. Simpler than complex complex is better than complicated.  Complexity trumps clutter flat is better than nested. Flat is better than nesting sparse is better than dense.  The interval is better than the compact readability counts. Readability is important special cases aren ' t special enough to break the rules. Even in the name of the practicality of the special case, it does not violate these rules although practicality beats purity. Although practicality is more than purity errors should never pass silently. Errors should not be silently ignored unless  Explicitly silenced. Unless there is clear silence in the face of the ambiguity, refuse the temptation to guess. When there are multiple possibilities, don't try to guess there should be one--and preferably only one--obvious-way-to-do it. There should be one, preferably a single, that can obviously do this although that way might not is obvious at first U   Nless you ' re Dutch. Although this approach may not be easy, unless you are the father of Python now better than never. Now do better than not do although never is often better than *right*. Although the past has never been better than now if the implementation is hard to explain, it's a bad idea.  If this implementation is not easily explained, then it must be a bad idea if the implementation are easy to explain, it can be a good ideas. If this implementation is easy to explain, then it's probably a good idea namespaces arE One honking great idea – let's do more of those! Namespaces are a wonderful idea and should be used more

  

Basic operations

F=open ('import this.txt'=print('first line:  '# read a line, the result for first line:beautiful is better than ugly.  =print# read entire file  # close 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

With statement

To avoid forgetting to close a file after opening it, you can automatically close it through the admin context, i.e.:

With open (' import this.txt','r') as F:         ...

PYTHON file 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.