Reprint: Python File Open Way detailed--a, A +, r+, w+ difference

Source: Internet
Author: User
Tags stdin

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 , 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 +:

[Python]View PlainCopyprint?
    1. <span style="Background-color:rgb (255, 255, 255); >FD = open ("1.txt",' w+ ')
    2. Fd.write (' 123 ')
    3. FD = open ("1.txt",' r+ ')
    4. Fd.write (' 456 ')
    5. FD = open ("1.txt",' A + ')
    6. Fd.write (' 789 ') </span>

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+

[Python]View PlainCopyprint?
  1. <span style="FONT-SIZE:14PX;" ><span style="Font-family:microsoft yahei;font-size:14px;" ><span style="Font-family:microsoft yahei;font-size:14px;" >>>> Fd=open (R' f:\mypython\test.py ',' W ') <span style="White-space:pre" > </span >#只读方式打开, read the error
  2. >>> Fd.read ()
  3. Traceback (most recent):
  4. File "<stdin>", line 1, in <module>
  5. Ioerror:file not open for reading
  6. >>> Fd=open (R' f:\mypython\test.py ',' a ') <span style="White-space:pre" > </span> #附加写方式打开, read the error
  7. >>> Fd.read ()
  8. Traceback (most recent):
  9. File "<stdin>", line 1, in <module>
  10. Ioerror:file not open for reading
  11. >>></span></span></span>

2. Correct read and write mode open, garbled

[Python]View PlainCopyprint?
    1. <span style="FONT-SIZE:14PX;" ><span style="Font-family:microsoft yahei;font-size:14px;" >>>> Fd=open (R' f:\mypython\test.py ',' A + ')
    2. >>> fd.write (' 123 ')
    3. >>> Fd.read ()
    4. ' \x02\xe0\xe9\xdc\x00\x00\x00\x00\x00\x00\ ' <pre name="code" class="python" >>>> Fd.close ()
    5. </span></span>

Close, manually open the file, nothing is written; After close, manually open the file, garbled: 123 chatteration?

Cause analysis: pointer problems. Open () opens an additional read-write mode file in a + mode, because it is a, so the pointer is at the end of the file. If you do read () at this point, Python discovers that the pointer position is EOF and reads to an empty string.

After writing 123, the position of the pointer is 4, still the end of the file, and the file is in memory 123[eof].

But it looks like read (), Python is still going to try to jump the pointer from the file header to 3 on the disk's file, then read to EOF.

In other words, you actually skipped the real EOF of the file, made a dump of the hard disk's underlying data, and dump it to the [EOF] of a previously saved file. So at last we get some random characters that don't look forward to, not the garbled ones caused by the coding problem.

Solution: Reset the pointer to a file header before reading (invalid if reset is read after reading)

[Python]View PlainCopyprint?
    1. <span style="FONT-SIZE:14PX;" ><span style="Font-family:microsoft yahei;font-size:14px;" >>>> Fd=open (R' f:\mypython\test.py ',' A + ')
    2. >>> Fd.seek (0)
    3. >>> Fd.read ()
    4. ' 123 ' <span style="White-space:pre" > </span>#顺利读出 </span></span>

3. The contents of the file, but read out the empty characters

[Python]View PlainCopy print?
  1. <span style="FONT-SIZE:14PX;" ><span style="Font-family:microsoft yahei;font-size:14px;" >>>> Fd=open (R' f:\mypython\test.py ',' w+ ') #清空内容, re-write
  2. >>> fd.write (' 456 ')
  3. >>> Fd.flush () <span style="White-space:pre" > </span>#确定写入, at which time the file content is "456"
  4. >>> Fd.read ()
  5. "<span style=" White-space:pre "> </span>#读出空 </span></span>

Cause: The same pointer problem, after writing the pointer to the end [EOF], so read out the empty

Solution One, call close and reopen, and the pointer is at the beginning. (R,r+,a+,u, be careful not to open with W,w+,a)

[Python]View PlainCopyprint?
  1. <span style="FONT-SIZE:14PX;" ><span style="Font-family:microsoft yahei;font-size:14px;" >>>> fd.close ()
  2. >>> Fd=open (R' f:\mypython\test.py ',' A + ')
  3. >>> Fd.read ()
  4. ' 456 '
  5. >>> Fd.close ()
  6. >>> Fd=open (R' f:\mypython\test.py ',' r+ ')
  7. >>> Fd.read ()
  8. ' 456 ' <pre name="code" class="python" >>>> fd.close ()
  9. >>> Fd=open (R' f:\mypython\test.py ',' R ')
  10. >>> Fd.read ()
  11. ' 456 '
  12. >>> Fd.close ()
  13. >>> Fd=open (R' f:\mypython\test.py ',' U ')
  14. >>> Fd.read ()
  15. ' 456 ' </span></span>

Solution Two, call seek to point to the beginning

[Python]View PlainCopyprint?
    1. <span style="FONT-SIZE:14PX;" ><span style="Font-family:microsoft yahei;font-size:14px;" >>>> Fd=open (R' f:\mypython\test.py ',' w+ ')
    2. >>> fd.write (' 456 ')
    3. >>> Fd.seek (0)
    4. >>> Fd.read ()
    5. ' 456 ' </span></span>

Seek function

Seek (offset[, whence]), offset is relative to a position. The position is determined by the whence, the default whence=0, starting from the beginning, whence=1 from the current position, whence=2 moving relative to the end of the file, and usually offset with a negative value.

Reprint: Python File Open Way detailed--a, A +, r+, w+ difference

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.