Access Modes for File Objects : open « File « Python Tutorial






File Mode  Operation
r          Open for read
rU or U      Open for read with universal NEWLINE support (PEP 278)
W          Open for write (truncate if necessary)
A          Open for append (always works from EOF, create if necessary)
r+          Open for read and write
w+          Open for read and write (see w above)
a+          Open for read and write (see a above)
Rb          Open for binary read
Wb          Open for binary write (see w above)
ab          Open for binary append (see a above)
rb+          Open for binary read and write (see r+ above)
wb+          Open for binary read and write (see w+ above)
ab+          Open for binary read and write (see a+ above)

Here are some examples for opening files:
fp = open('/etc/motd')        #open file for read
fp = open('test', 'w')        #open file for write
fp = open('data', 'r+')       #open file for read/write
fp = open(r'c:\io.sys', 'rb') #open binary file for read








12.9.open
12.9.1.Access Modes for File Objects
12.9.2.'r' is the default processing mode
12.9.3.Open a file for reading
12.9.4.Open a file for writing
12.9.5.Opening a Nonexistent File
12.9.6.Opening a File and check its mode
12.9.7.Reading and Writing