<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def construct_listener(outfile=None):
 """Create the listener that prints tweets"""

    if outfile is not None:
        if os.path.exists(outfile):
            raise IOError("File %s already exists" % outfile)
        
        outfile = open(outfile, 'wb')

    return PrintingListener(out=outfile)