def openfile(path, mode="r", *args, **kw):
    """Open file

    Aside from the normal modes accepted by `open()`, this function
    accepts an `x` mode that causes the file to be opened for exclusive-
    write, which means that an exception (`FileExists`) will be raised
    if the file being opened already exists.
    """
    if "x" not in mode or sys.version_info > (3, 0):
        return open(path, mode, *args, **kw)
    # http://stackoverflow.com/a/10979569/10840
    # O_EXCL is only supported on NFS when using NFSv3 or later on kernel 2.6 or later.
    flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
    try:
        handle = os.open(path, flags)
    except __HOLE__ as err:
        if err.errno == errno.EEXIST:
            raise FileExists(path)
        raise
    return os.fdopen(handle, mode.replace("x", "w"), *args, **kw)
dataset/ETHPy150Open dimagi/commcare-hq/corehq/blobs/fsdb.py/openfile