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

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » File » open 
12.9.1.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
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.