Python file Open in detail--a, A +, r+, w+ differences

Source: Internet
Author: User
Tags readable stdin

Source: http://blog.csdn.net/ztf312/

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 0

(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 Plaincopy print?
    1. 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 ')
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+

[Python]View Plaincopy print?
  1. >>> Fd=open (R' f:\mypython\test.py ',' W ') #只读方式打开, 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 ')#附加写方式打开, 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>
>>> Fd=open (R ' f:\mypython\test.py ', ' W ') #只读方式打开, read error >>> Fd.read () Traceback (most recent call last):  File "<stdin>", line 1, in <module>ioerror:file not open for reading>>> Fd=open (R ' f:\mypython\ test.py ', ' a ') #附加写方式打开, read error >>> Fd.read () Traceback (most recent call last):  File "<stdin>", line 1, in <module>ioerror:file not open for reading>>></span></span></span>

2. Correct read and write mode open, garbled

[Python]View Plaincopy print?
    1. >>> Fd=open (R' f:\mypython\test.py ',' A + ')
    2. >>> fd.write (' 123 ')
    3. >>> Fd.read ()
    4. >>> Fd.close ()
>>> Fd=open (R ' f:\mypython\test.py ', ' A + ') >>> fd.write (' 123 ') >>> Fd.read () >>> Fd.close ()

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 Plaincopy print?
    1. >>> 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>
>>> Fd=open (R ' f:\mypython\test.py ', ' A + ') >>> fd.seek (0) >>> fd.read () ' 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. >>> 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. " #读出空
>>> Fd=open (R ' f:\mypython\test.py ', ' w+ ') #清空内容, re-write >>> fd.write (' 456 ') >>> Fd.flush () <span style= "White-space:pre" ></span> #确定写入, at which time the file content is "456" >>> fd.read () ' #读出空

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 Plaincopy print?
  1. >>> 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 '
>>> fd.close () >>> Fd=open (R ' f:\mypython\test.py ', ' A + ') >>> fd.read () ' 456 ' >>> Fd.close () >>> Fd=open (R ' f:\mypython\test.py ', ' r+ ') >>> fd.read () ' 456 ' <pre name= "code" class= " Python ">>>> fd.close () >>> Fd=open (R ' f:\mypython\test.py ', ' R ') >>> Fd.read () ' 456 ' > >> fd.close () >>> Fd=open (R ' f:\mypython\test.py ', ' U ') >>> fd.read () ' 456 '

Solution Two, call seek to point to the beginning

[Python]View Plaincopy print?
    1. >>> Fd=open (R' f:\mypython\test.py ',' w+ ')
    2. >>> fd.write (' 456 ')
    3. >>> Fd.seek (0)
    4. >>> Fd.read ()
    5. ' 456 '
>>> Fd=open (R ' f:\mypython\test.py ', ' w+ ') >>> fd.write (' 456 ') >>> fd.seek (0) >>> Fd.read () ' 456 '

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.

4. Remember close () closes

When we write a file, the operating system often does not immediately write the data to disk, but instead put it in memory cache, and then write slowly when idle. Only when the close () method is called does the operating system guarantee that all data that is not written is written to disk. The consequence of forgetting to call Close () is that the data may have been written only partially to the disk, and the remainder has been lost. Therefore, it is safe to use the WITH statement:

With open ('/users/michael/test.txt ', ' W ') as F:f.write (' Hello, world! ')

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.