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?
- FD = open ("1.txt",' w+ ')
- Fd.write (' 123 ')
- FD = open ("1.txt",' r+ ')
- Fd.write (' 456 ')
- FD = open ("1.txt",' A + ')
- 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?
- >>> Fd=open (R' f:\mypython\test.py ',' W ') #只读方式打开, read the error
- >>> Fd.read ()
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- Ioerror:file not open for reading
- >>> Fd=open (R' f:\mypython\test.py ',' a ')#附加写方式打开, read the error
- >>> Fd.read ()
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- Ioerror:file not open for reading
- >>></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?
- >>> Fd=open (R' f:\mypython\test.py ',' A + ')
- >>> fd.write (' 123 ')
- >>> Fd.read ()
- >>> 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?
- >>> Fd=open (R' f:\mypython\test.py ',' A + ')
- >>> Fd.seek (0)
- >>> Fd.read ()
- ' 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?
- >>> 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 ()
- " #读出空
>>> 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?
- >>> 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 '
>>> 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?
- >>> Fd=open (R' f:\mypython\test.py ',' w+ ')
- >>> fd.write (' 456 ')
- >>> Fd.seek (0)
- >>> Fd.read ()
- ' 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