How to open python file--r,w,a,r+,w+,a+

Source: Internet
Author: User
Tags readable stdin

r,r+,w,w+,a,a+ effect

R Read-only Do not create
r+ Write

W New Write-only Both will empty the file
w+ New Read/write

A Attach write mode open, unreadable
A + Additional read and write mode open


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')#write-only 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>

2. Correct read and write mode open, garbled

>>> 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)

>>> Fd=open (R'f:\mypython\test.py','a+')   >>> fd.seek (0)  >>> fd.read ()  '123 '<span style='white-space:pre'>           </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)

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

>>> Fd=open (R'f:\mypython\test.py','w+')  # w+ will clear the file first so you need to rewrite the content >>> 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!.') with open ('Log','R', encoding='UTF8') as F:f.readlines () F.read ()#py2.7 later with Add method, with simultaneous management of multiple file objectsWith open ('Log1','R', encoding='UTF8') as F_read,open ('log2','W', encoding='UTF8') as F_write: forLineinchF_read:f_write.write (line)

Reference:

47259805

How to open python file--r,w,a,r+,w+,a+

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.