You may have noticed that there are two statements that create a file descriptor object. One is for flat text log files and the other one is for files compressed with bzip2. The differentiator is the log file extension, which in case of bzip2 compression is .bz2.
Python includes a bzip2-handling module as part of a standard set of packages. The most useful class in the module is BZ2File, which implements a full interface for handling compressed files. You can use it just as you would use the standard Open function. The returned object is a file descriptor object that implements standard file-handling operations: read, readline, write, writeline, seek and close.
Since the only difference is in how the file descriptor object is created, even though I'm using a different function to get the object, the result is assigned to the same fd variable that will be used later in the code.
Was this article helpful?