io_handling.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:aws-encryption-sdk-cli 作者: awslabs 项目源码 文件源码
def _should_write_file(self, filepath):
        # type: (str) -> bool
        """Determines whether a specific file should be written.

        :param str filepath: Full file path to file in question
        :rtype: bool
        """
        if not os.path.isfile(filepath):
            # The file does not exist, nothing to overwrite
            return True

        if self.no_overwrite:
            # The file exists and the caller specifically asked us not to overwrite anything
            _LOGGER.warning('Skipping existing output file because of "no overwrite" option: %s', filepath)
            return False

        if self.interactive:
            # The file exists and the caller asked us to be consulted on action before overwriting
            decision = six.moves.input(  # type: ignore # six.moves confuses mypy
                'Overwrite existing output file "{}" with new contents? [y/N]:'.format(filepath)
            )
            try:
                if decision.lower()[0] == 'y':
                    _LOGGER.warning('Overwriting existing output file based on interactive user decision: %s', filepath)
                    return True
                return False
            except IndexError:
                # No input is interpreted as 'do not overwrite'
                _LOGGER.warning('Skipping existing output file based on interactive user decision: %s', filepath)
                return False

        # If we get to this point, the file exists and we should overwrite it
        _LOGGER.warning('Overwriting existing output file because no action was specified otherwise: %s', filepath)
        return True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号