コード例 #1
0
def append_file_contents(file_path, contents):
    """
    Append 'contents' to 'file_path'.
    """
    bytes_to_write = encode_for_writing_to_file(contents)
    try:
        with open(file_path, "ab+") as F:
            F.write(bytes_to_write)
    except EnvironmentError as e:
        logger.error_with_prefix(
            'AppendFileContents',
            'Appending to file ' + file_path + ' Exception is ' + str(e))
        return None
    return 0
コード例 #2
0
def set_file_contents(file_path, contents):
    """
    Write 'contents' to 'file_path'.
    """
    if type(contents) == str:
        contents = contents.encode('latin-1', 'ignore')
    try:
        with open(file_path, "wb+") as F:
            F.write(contents)
    except OSError as e:
        logger.error_with_prefix(
            'SetFileContents', 'Writing to file ' + file_path + ' Exception is ' + str(e))
        return None
    return 0
コード例 #3
0
def replace_file_with_contents_atomic(filepath, contents):
    """
    Write 'contents' to 'filepath' by creating a temp file, and replacing original.
    """
    handle, temp = tempfile.mkstemp(dir=os.path.dirname(filepath))
    if type(contents) == str:
        contents = contents.encode('latin-1')
    try:
        os.write(handle, contents)
    except OSError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic', 'Writing to file ' + filepath + ' Exception is ' + str(e))
        return None
    finally:
        os.close(handle)
    try:
        os.rename(temp, filepath)
        return None
    except OSError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic', 'Renaming ' + temp + ' to ' + filepath + ' Exception is ' + str(e)
        )
    try:
        os.remove(filepath)
    except OSError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic', 'Removing ' + filepath + ' Exception is ' + str(e))
    try:
        os.rename(temp, filepath)
    except OSError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic', 'Removing ' + filepath + ' Exception is ' + str(e))
        return 1
    return 0
コード例 #4
0
def get_file_contents(file_path, as_bin=False):
    """
    Read and return contents of 'file_path'.
    """
    mode = 'r'
    if as_bin:
        mode += 'b'
    try:
        with open(file_path, mode) as F:
            contents = F.read()
            return contents
    except OSError as e:
        logger.error_with_prefix(
            'GetFileContents', 'Reading from file ' + file_path + ' Exception is ' + str(e))
        return None
コード例 #5
0
def append_file_contents(file_path, contents):
    """
    Append 'contents' to 'file_path'.
    """
    if type(contents) == str:
        if sys.version_info[0] == 3:
            contents = contents.encode('latin-1').decode('latin-1')
        elif sys.version_info[0] == 2:
            contents = contents.encode('latin-1')
    try:
        with open(file_path, "a+") as F:
            F.write(contents)
    except OSError as e:
        logger.error_with_prefix(
            'AppendFileContents', 'Appending to file ' + file_path + ' Exception is ' + str(e))
        return None
    return 0
コード例 #6
0
def replace_file_with_contents_atomic(filepath, contents):
    """
    Write 'contents' to 'filepath' by creating a temp file, and replacing original.
    """
    handle, temp = tempfile.mkstemp(dir=os.path.dirname(filepath))
    bytes_to_write = encode_for_writing_to_file(contents)
    try:
        os.write(handle, bytes_to_write)
    except EnvironmentError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic',
            'Writing to file ' + filepath + ' Exception is ' + str(e))
        return None
    finally:
        os.close(handle)
    try:
        os.rename(temp, filepath)
        return None
    except EnvironmentError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic',
            'Renaming ' + temp + ' to ' + filepath + ' Exception is ' + str(e))
    try:
        os.remove(filepath)
    except EnvironmentError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic',
            'Removing ' + filepath + ' Exception is ' + str(e))
    try:
        os.rename(temp, filepath)
    except EnvironmentError as e:
        logger.error_with_prefix(
            'ReplaceFileContentsAtomic',
            'Removing ' + filepath + ' Exception is ' + str(e))
        return 1
    return 0