Mega Code Archive

 
Categories / Python Tutorial / File
 

Access Modes for File Objects

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