扫描单个路径 返回: (文件列表, 总大小)
(self, path: str, rule: Dict)
| 854 | return False |
| 855 | |
| 856 | def scan_path(self, path: str, rule: Dict) -> Tuple[List[str], int]: |
| 857 | """ |
| 858 | 扫描单个路径 |
| 859 | 返回: (文件列表, 总大小) |
| 860 | """ |
| 861 | files = [] |
| 862 | total_size = 0 |
| 863 | |
| 864 | if not os.path.exists(path): |
| 865 | return files, total_size |
| 866 | |
| 867 | try: |
| 868 | # 如果是文件 |
| 869 | if os.path.isfile(path): |
| 870 | if not self.should_skip_file(path, rule): |
| 871 | # 检查扩展名 |
| 872 | if rule.get('ext'): |
| 873 | if any(path.endswith(ext) or ext in os.path.basename(path) |
| 874 | for ext in rule['ext']): |
| 875 | size = os.path.getsize(path) |
| 876 | files.append(path) |
| 877 | total_size += size |
| 878 | else: |
| 879 | size = os.path.getsize(path) |
| 880 | files.append(path) |
| 881 | total_size += size |
| 882 | |
| 883 | # 如果是目录 |
| 884 | elif os.path.isdir(path): |
| 885 | for root, dirs, filenames in os.walk(path): |
| 886 | for filename in filenames: |
| 887 | filepath = os.path.join(root, filename) |
| 888 | |
| 889 | if self.should_skip_file(filepath, rule): |
| 890 | continue |
| 891 | |
| 892 | try: |
| 893 | # 检查扩展名 |
| 894 | if rule.get('ext'): |
| 895 | if any(filename.endswith(ext) or ext in filename |
| 896 | for ext in rule['ext']): |
| 897 | size = os.path.getsize(filepath) |
| 898 | files.append(filepath) |
| 899 | total_size += size |
| 900 | else: |
| 901 | size = os.path.getsize(filepath) |
| 902 | files.append(filepath) |
| 903 | total_size += size |
| 904 | except (OSError, PermissionError): |
| 905 | continue |
| 906 | |
| 907 | except (OSError, PermissionError): |
| 908 | pass |
| 909 | |
| 910 | return files, total_size |
| 911 | |
| 912 | def scan_category(self, category_name: str, show_progress: bool = True) -> Dict: |
| 913 | """扫描一个分类的所有规则""" |
no test coverage detected