3.11. File objects

Python has a built-in function, open, for opening a file on disk. open returns a file object, which has methods and attributes for getting information about and manipulating the opened file.

Example 3.23. Opening a file

>>> f = open("/music/_singles/kairo.mp3", "rb") 1
>>> f                                           2
<open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
>>> f.mode                                      3
>>> f.name                                      4
1 The open method can take up to three parameters: a filename, a mode, and a buffering parameter. Only the first one, the filename, is required; the other two are optional. If not specified, the file is opened for reading in text mode. Here we are opening the file for reading in binary mode. (print open.__doc__ displays a great explanation of all the possible modes.)
2 The open function returns an object (by now, this should not surprise you). A file object has several useful attributes.
3 The mode attribute of a file object tells you what mode the file was opened in.
4 The name attribute of a file object tells you the name of the file that the file object has open.

Example 3.24. Reading a file

>>> f
<open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
>>> f.tell()              1
>>> f.seek(-128, 2)       2
>>> f.tell()              3
>>> tagData = f.read(128) 4
>>> tagData
'TAGKAIRO****THE BEST GOA         ***DJ MARY-JANE***            Rave Mix                      2000http://mp3.com/DJMARYJANE     \037'
>>> f.tell()              5
1 A file object maintains state about the file it has open. The tell method of a file object tells you your current position in the open file. Since we haven’t done anything with this file yet, the current position is 0, which is the beginning of the file.
2 The seek method of a file object moves to another position in the open file. The second parameter specifies what the first one means; 0 means move to an absolute position (counting from the start of the file), 1 means move to a relative position (counting from the current position), and 2 means move to a position relative to the end of the file. Since the MP3 tags we’re looking for are stored at the end of the file, we use 2 and tell the file object to move to a position 128 bytes from the end of the file.
3 The tell method confirms that the current file position has moved.
4 The read method reads a specified number of bytes from the open file and returns a string with the data which was read. The optional parameter specifies the maximum number of bytes to read. If no parameter is specified, read will read until the end of the file. (We could have simply said read() here, since we know exactly where we are in the file and we are, in fact, reading the last 128 bytes.) The read data is assigned to the tagData variable, and the current position is updated based on how many bytes were read.
5 The tell method confirms that the current position has moved. If you do the math, you’ll see that after reading 128 bytes, the position has been incremented by 128.

Example 3.25. Closing a file

>>> f
<open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
>>> f.closed  1
>>> f.close() 2
>>> f
<closed file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
>>> f.closed
>>> f.seek(0) 3
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
ValueError: I/O operation on closed file
>>> f.tell()
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
ValueError: I/O operation on closed file
>>> f.read()
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
ValueError: I/O operation on closed file
>>> f.close() 4
1 The closed attribute of a file object indicates whether the object has a file open or not. In this case, the file is still open (closed is 0). Open files consume system resources, and depending on the file mode, other programs may not be able to access them. It’s important to close files as soon as you’re done with them.
2 To close a file, call the close method of the file object. This frees the lock (if any) that you were holding on the file, flushes buffered writes (if any) that the system hadn’t gotten around to actually writing yet, and releases the system resources. The closed attribute confirms that the file is closed.
3 Just because a file is closed doesn’t mean that the file object ceases to exist. The variable f will continue to exist until it goes out of scope or gets manually deleted. However, none of the methods that manipulate an open file will work once the file has been closed; they all raise an exception.
4 Calling close on a file object whose file is already closed does not raise an exception; it fails silently.

Example 3.26. File objects in MP3FileInfo

        try:                                1
            fsock = open(filename, "rb", 0) 2
                fsock.seek(-128, 2)         3
                tagdata = fsock.read(128)   4
            finally:                        5
        except IOError:                     6
1 Because opening and reading files is risky and may raise an exception, all of this code is wrapped in a try...except block. (Hey, isn’t standardized indentation great? This is where you start to appreciate it.)
2 The open function may raise an IOError. (Maybe the file doesn’t exist.)
3 The seek method may raise an IOError. (Maybe the file is smaller than 128 bytes.)
4 The read method may raise an IOError. (Maybe the disk has a bad sector, or it’s on a network drive and the network just went down.)
5 This is new: a try...finally block. Once the file has been opened successfully by the open function, we want to make absolutely sure that we close it, even if an exception is raised by the seek or read methods. That’s what a try...finally block is for: code in the finally block will always be executed, even if something in the try block raises an exception. Think of it as code that gets executed “on the way out”, regardless of what happened on the way.
6 At last, we handle our IOError exception. This could be the IOError exception raised by the call to open, seek, or read. Here, we really don’t care, because all we’re going to do is ignore it silently and continue. (Remember, pass is a Python statement that does nothing.) That’s perfectly legal; “handling” an exception can mean explicitly doing nothing. It still counts as handled, and processing will continue normally on the next line of code after the try...except block.

Further reading