Files and File Objects
The built-in function open(name [,mode [,bufsize]]) opens and creates a file object, as shown here:
f = open('foo') # Opens 'foo' for reading f = open('foo','r') # Opens 'foo' for reading (same as above) f = open('foo','w') # Open for writing
Although less common, files can also be created by calling the file object constructor, which is identical to open(). For example:
f = file('foo') # Opens 'foo' for reading f = file('foo','w') # Open for writing
The file mode is ‘r’ for read, ‘w’ for write, or ‘a’ for append. The mode character can be followed by ‘b’ for binary data, such as ‘rb’ or ‘wb’. This is optional on UNIX, but it’s required on Windows and should be included if you are concerned about portability. In addition, ...
Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.