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