示例#1
0
def safe_write(filename, mode='wb', compresslevel=5):
    """ 
        Makes atomic writes by writing to a temp filename. 
        Also if the filename ends in ".gz", writes to a compressed stream.
        Yields a file descriptor.
    """
    from procgraph.block_utils.file_io import make_sure_dir_exists
    make_sure_dir_exists(filename)

    tmp_filename = '%s.tmp' % filename
    try:
        if is_gzip_filename(filename):
            fopen = lambda f, mode: gzip.open(
                filename=f, mode=mode, compresslevel=compresslevel)
        else:
            fopen = open

        with fopen(tmp_filename, mode) as f:
            yield f

        os.rename(tmp_filename, filename)
    except:
        if os.path.exists(tmp_filename):
            os.unlink(tmp_filename)
        if os.path.exists(filename):
            os.unlink(filename)
        raise
示例#2
0
def safe_write(filename, mode="wb", compresslevel=5):
    """ 
        Makes atomic writes by writing to a temp filename. 
        Also if the filename ends in ".gz", writes to a compressed stream.
        Yields a file descriptor.
    """
    from procgraph.block_utils.file_io import make_sure_dir_exists

    make_sure_dir_exists(filename)

    tmp_filename = "%s.tmp" % filename
    try:
        if is_gzip_filename(filename):
            fopen = lambda f, mode: gzip.open(filename=f, mode=mode, compresslevel=compresslevel)
        else:
            fopen = open

        with fopen(tmp_filename, mode) as f:
            yield f

        os.rename(tmp_filename, filename)
    except:
        if os.path.exists(tmp_filename):
            os.unlink(tmp_filename)
        if os.path.exists(filename):
            os.unlink(filename)
        raise
示例#3
0
def write_string_to_file(s, filename):
    make_sure_dir_exists(filename)
    with open(filename, 'w') as f:
        f.write(s)