def backup_file(filePath, maxBackups=10): """ Create backup for the file. File.txt -> File_1.txt Parameters: 1. filePath: File to be backed up 2. maxBackups: Maximum number of backups to create in the location Return: 1. returnValue: True if file back up is successful """ returnValue = False filename, extention = os.path.splitext(filePath) if c_path.validate_file(filePath) and c_path.validate_file_write(filePath): for index in reversed(range(0, maxBackups)): backup_file_path = filename + '_{0}'.format(index + 1) + extention origFile = filename + '_{0}'.format(index) + extention if c_path.validate_file(backup_file_path): try: os.remove(backup_file_path) except Exception: logger.debug(traceback.format_exc()) raise CoreError( CoreErrorCode.GENERIC_FAILURE, 'Removing file: {0}'.format(sys.exc_info()[1])) if c_path.validate_file(origFile): try: os.rename(origFile, backup_file_path) except Exception: logger.debug(traceback.format_exc()) raise CoreError( CoreErrorCode.GENERIC_FAILURE, 'Renaming file: {0}'.format(sys.exc_info()[1])) backup_file_path = filename + '_{0}'.format(1) + extention f_retValue, f_retErr = c_path.copyFile(filePath, backup_file_path, force=True) if not f_retValue: raise CoreError(CoreErrorCode.GENERIC_FAILURE, 'Backing up: {0}'.format(f_retErr)) else: returnValue = True return returnValue
def backup_file(filePath, maxBackups=10): """ Create backup for the file. File.txt -> File_1.txt Parameters: 1. filePath: File to be backed up 2. maxBackups: Maximum number of backups to create in the location Return: 1. returnValue: True if file back up is successful """ returnValue = False filename, extention = os.path.splitext(filePath) if c_path.validate_file(filePath) and c_path.validate_file_write(filePath): for index in reversed(range(0, maxBackups)): backup_file_path = filename + '_{0}'.format(index + 1) + extention origFile = filename + '_{0}'.format(index) + extention if c_path.validate_file(backup_file_path): try: os.remove(backup_file_path) except Exception: logger.debug(traceback.format_exc()) raise CoreError(CoreErrorCode.GENERIC_FAILURE, 'Removing file: {0}'.format(sys.exc_info()[1])) if c_path.validate_file(origFile): try: os.rename(origFile, backup_file_path) except Exception: logger.debug(traceback.format_exc()) raise CoreError(CoreErrorCode.GENERIC_FAILURE, 'Renaming file: {0}'.format(sys.exc_info()[1])) backup_file_path = filename + '_{0}'.format(1) + extention f_retValue, f_retErr = c_path.copyFile(filePath, backup_file_path, force=True) if not f_retValue: raise CoreError(CoreErrorCode.GENERIC_FAILURE, 'Backing up: {0}'.format(f_retErr)) else: returnValue = True return returnValue