"Go" python File open in detail--a, A +, r+, w+ differences

Source: Internet
Author: User
Tags stdin

Original address: http://blog.csdn.net/ztf312/article/details/47259805

The first step to exclude file open mode error:

R Read-only , r+ read/write, not created

W New write-only , w+ New Read-write , both will clear the contents of the file

(opens in W mode and cannot be read out.) w+ Readable and writable)

the difference between **w+ and r+ :

r+: Readable and writable, if the file does not exist, error; w+: Readable and writable, if the file does not exist, create

the difference between r+ and A + :

FD = open ("1.txt",'w+') Fd.write ('123') FD= Open ("1.txt",'r+') Fd.write ('456') FD= Open ("1.txt",'A +') Fd.write ('789')

Results: 456789

Description r+ the overwrite write.

Open the file in a a,a+ way, attach it

(a: additional write mode open, unreadable; a+: additional read and write mode open)

Open the file with the ' U ' flag, and all the line separators are returned with a newline character (such as read* ()) by means of the Python input method (example # ()). (' RU ' mode also supports ' RB ' option).

R and u require that the file must exist

non-readable open mode :W and a

How to open a new file if it does not exist: a,a+,w,w+

>>> Fd=open (r'f:\mypython\test.py','W')#Read -only mode open, read error>>>Fd.read () Traceback (most recent): File"<stdin>", Line 1,inch<module>Ioerror:file notOpen forReading>>> Fd=open (r'f:\mypython\test.py','a')#additional write mode open, read error>>>Fd.read () Traceback (most recent): File"<stdin>", Line 1,inch<module>Ioerror:file notOpen forReading>>></span></span></span>

"Go" python File open in detail--a, A +, r+, w+ differences

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.