code: def must_open(filename, mode="r", checkexists=False, skipcheck=False, \
              oappend=False):
      
      if isinstance(filename, list):
          assert "r" in mode
  
          if filename[0].endswith((".gz", ".bz2")):
              filename = " ".join(filename)  # allow opening multiple gz/bz2 files
          else:
              import fileinput
              return fileinput.input(filename)
  
      if filename.startswith("s3://"):
          from jcvi.utils.aws import pull_from_s3
          filename = pull_from_s3(filename)
  
      if filename in ("-", "stdin"):
          assert "r" in mode
          fp = sys.stdin
  
      elif filename == "stdout":
          assert "w" in mode
          fp = sys.stdout
  
      elif filename == "stderr":
          assert "w" in mode
          fp = sys.stderr
  
      elif filename == "tmp" and mode == "w":
          from tempfile import NamedTemporaryFile
          fp = NamedTemporaryFile(delete=False)
  
      elif filename.endswith(".gz"):
          if 'r' in mode:
              cmd = "gunzip -c {0}".format(filename)
              fp = popen(cmd, debug=False)
          elif 'w' in mode:
              import gzip
              fp = gzip.open(filename, mode)
  
      elif filename.endswith(".bz2"):
          if 'r' in mode:
              cmd = "bzcat {0}".format(filename)
              fp = popen(cmd, debug=False)
          elif 'w' in mode:
              import bz2
              fp = bz2.BZ2File(filename, mode)
  
      else:
          if checkexists:
              assert mode == "w"
              overwrite = (not op.exists(filename)) if skipcheck \
                          else check_exists(filename, oappend)
              if overwrite:
                  if oappend:
                      fp = open(filename, "a")
                  else:
                      fp = open(filename, "w")
              else:
                  logging.debug("File `{0}` already exists. Skipped."\
                          .format(filename))
                  return None
          else:
              fp = open(filename, mode)
  
      return fp
docs: Accepts filename and returns filehandle.
  
      Checks on multiple files, stdin/stdout/stderr, .gz or .bz2 file.