scanner.py 文件源码

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

项目:dirtools3 作者: kirpit 项目源码 文件源码
def _get_attributes(self, item: os.DirEntry) -> dict:
        """Parses entire item and subdirectories and returns:

        * Total size in bytes
        * Maximum folder depth of item
        * Total number of files this item contains
        * Access timestamp
        * Modification timestamp
        * Change timestamp

        in the same order as tuple.

        :param item: DirEntry object
        :type item: posix.DirEntry
        :return: Dictionary of {size, depth, num_of_files, atime, mtime, ctime}
        :rtype: dict
        """
        # it's a file or symlink, size is already on item stat
        if not item.is_dir(follow_symlinks=False):
            stat = item.stat(follow_symlinks=False)
            return {'size': stat.st_size,
                    'depth': self._get_depth(item.path) - self._level,
                    'num_of_files': 1,
                    'atime': int(stat.st_atime),
                    'mtime': int(stat.st_mtime),
                    'ctime': int(stat.st_ctime)}

        # It is a folder, recursive size check
        else:
            total_size = num_of_files = depth = 0
            atime = mtime = ctime = 0

            with os.scandir(item.path) as directory:
                for i in directory:
                    attrs = self._get_attributes(i)
                    total_size += attrs['size']
                    num_of_files += attrs['num_of_files']
                    atime = max(atime, attrs['atime'])
                    mtime = max(mtime, attrs['mtime'])
                    ctime = max(ctime, attrs['ctime'])
                    depth = max(depth, attrs['depth'])

            return {'size': total_size,
                    'depth': depth,
                    'num_of_files': num_of_files,
                    'atime': atime,
                    'mtime': mtime,
                    'ctime': ctime}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号