cm-filekit 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: cm_filekit
3
+ Version: 1.0.0
4
+ Summary: 计算概论C课程文件夹扫描、统计、整理和安全读写工具
5
+ Author: Chen Mo
6
+ Author-email: chenmoemail@126.com
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Natural Language :: Chinese (Simplified)
11
+ Classifier: Intended Audience :: Education
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: requires-python
20
+ Dynamic: summary
21
+
22
+ # cm_filekit
23
+
24
+ 《计算概论C》文件与目录任务工具库,适合文件读写和异常处理之后使用。
25
+
26
+ ```bash
27
+ pip install cm_filekit
28
+ ```
29
+
30
+ ```python
31
+ import cm_filekit
32
+
33
+ print(cm_filekit.count_by_extension('.'))
34
+ print(cm_filekit.find_large_files('.', min_mb=20))
35
+
36
+ report = cm_filekit.make_file_report('.')
37
+ print(report)
38
+
39
+ moves = cm_filekit.organize_by_extension('.', dry_run=True)
40
+ cm_filekit.write_csv('整理预览.csv', moves, header=['原文件', '新位置'])
41
+ ```
42
+
43
+ 这个库不替代 `open()`、循环和路径知识,而是把真实文件夹任务做得更容易上手。
@@ -0,0 +1,22 @@
1
+ # cm_filekit
2
+
3
+ 《计算概论C》文件与目录任务工具库,适合文件读写和异常处理之后使用。
4
+
5
+ ```bash
6
+ pip install cm_filekit
7
+ ```
8
+
9
+ ```python
10
+ import cm_filekit
11
+
12
+ print(cm_filekit.count_by_extension('.'))
13
+ print(cm_filekit.find_large_files('.', min_mb=20))
14
+
15
+ report = cm_filekit.make_file_report('.')
16
+ print(report)
17
+
18
+ moves = cm_filekit.organize_by_extension('.', dry_run=True)
19
+ cm_filekit.write_csv('整理预览.csv', moves, header=['原文件', '新位置'])
20
+ ```
21
+
22
+ 这个库不替代 `open()`、循环和路径知识,而是把真实文件夹任务做得更容易上手。
@@ -0,0 +1,28 @@
1
+ """cm_filekit - 文件夹扫描、统计、整理和安全读写工具。"""
2
+
3
+ from .core import (
4
+ scan_files,
5
+ count_by_extension,
6
+ make_file_report,
7
+ find_empty_files,
8
+ find_large_files,
9
+ preview_rename,
10
+ organize_by_extension,
11
+ read_text_smart,
12
+ write_text,
13
+ write_csv,
14
+ )
15
+
16
+ __version__ = '1.0.0'
17
+ __all__ = [
18
+ 'scan_files',
19
+ 'count_by_extension',
20
+ 'make_file_report',
21
+ 'find_empty_files',
22
+ 'find_large_files',
23
+ 'preview_rename',
24
+ 'organize_by_extension',
25
+ 'read_text_smart',
26
+ 'write_text',
27
+ 'write_csv',
28
+ ]
@@ -0,0 +1,97 @@
1
+ import csv
2
+ import shutil
3
+ from collections import Counter
4
+ from pathlib import Path
5
+
6
+
7
+ def scan_files(folder, recursive=True):
8
+ """扫描文件夹,返回文件路径字符串列表。"""
9
+ root = Path(folder)
10
+ pattern = '**/*' if recursive else '*'
11
+ return [str(path) for path in root.glob(pattern) if path.is_file()]
12
+
13
+
14
+ def count_by_extension(folder, recursive=True):
15
+ """统计文件夹中各类扩展名数量。"""
16
+ files = scan_files(folder, recursive=recursive)
17
+ counter = Counter(Path(file).suffix.lower() or '[无扩展名]' for file in files)
18
+ return dict(sorted(counter.items()))
19
+
20
+
21
+ def make_file_report(folder, recursive=True):
22
+ """生成文件夹概览报告。"""
23
+ files = [Path(file) for file in scan_files(folder, recursive=recursive)]
24
+ total_size = sum(file.stat().st_size for file in files)
25
+ return {
26
+ 'folder': str(Path(folder)),
27
+ 'file_count': len(files),
28
+ 'total_size_bytes': total_size,
29
+ 'extensions': count_by_extension(folder, recursive=recursive),
30
+ }
31
+
32
+
33
+ def find_empty_files(folder, recursive=True):
34
+ """查找大小为 0 的空文件。"""
35
+ return [file for file in scan_files(folder, recursive=recursive) if Path(file).stat().st_size == 0]
36
+
37
+
38
+ def find_large_files(folder, min_mb=10, recursive=True):
39
+ """查找大于 min_mb MB 的文件。"""
40
+ limit = min_mb * 1024 * 1024
41
+ return [file for file in scan_files(folder, recursive=recursive) if Path(file).stat().st_size >= limit]
42
+
43
+
44
+ def preview_rename(files, prefix='', start=1, width=2):
45
+ """预览批量重命名结果,不真正改名。"""
46
+ result = []
47
+ for index, file in enumerate(files, start=start):
48
+ path = Path(file)
49
+ new_name = f'{prefix}{index:0{width}d}{path.suffix}'
50
+ result.append((str(path), str(path.with_name(new_name))))
51
+ return result
52
+
53
+
54
+ def organize_by_extension(folder, target_folder=None, recursive=False, dry_run=True):
55
+ """按扩展名整理文件。dry_run=True 时只预览,不移动文件。"""
56
+ root = Path(folder)
57
+ target = Path(target_folder) if target_folder else root / '整理结果'
58
+ moves = []
59
+ for file in [Path(p) for p in scan_files(root, recursive=recursive)]:
60
+ ext = file.suffix.lower().lstrip('.') or '无扩展名'
61
+ new_path = target / ext / file.name
62
+ moves.append((str(file), str(new_path)))
63
+ if not dry_run:
64
+ new_path.parent.mkdir(parents=True, exist_ok=True)
65
+ shutil.move(str(file), str(new_path))
66
+ return moves
67
+
68
+
69
+ def read_text_smart(filename):
70
+ """尝试用常见编码读取文本,减少中文乱码。"""
71
+ path = Path(filename)
72
+ for encoding in ('utf-8', 'utf-8-sig', 'gbk', 'gb18030'):
73
+ try:
74
+ return path.read_text(encoding=encoding)
75
+ except UnicodeDecodeError:
76
+ continue
77
+ return path.read_text(encoding='utf-8', errors='ignore')
78
+
79
+
80
+ def write_text(filename, text):
81
+ """用 UTF-8 写入文本,自动创建父目录。"""
82
+ path = Path(filename)
83
+ path.parent.mkdir(parents=True, exist_ok=True)
84
+ path.write_text(str(text), encoding='utf-8')
85
+ return str(path)
86
+
87
+
88
+ def write_csv(filename, rows, header=None):
89
+ """写入 CSV 文件,适合 Excel 打开。"""
90
+ path = Path(filename)
91
+ path.parent.mkdir(parents=True, exist_ok=True)
92
+ with path.open('w', encoding='utf-8-sig', newline='') as f:
93
+ writer = csv.writer(f)
94
+ if header:
95
+ writer.writerow(header)
96
+ writer.writerows(rows)
97
+ return str(path)
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: cm_filekit
3
+ Version: 1.0.0
4
+ Summary: 计算概论C课程文件夹扫描、统计、整理和安全读写工具
5
+ Author: Chen Mo
6
+ Author-email: chenmoemail@126.com
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Natural Language :: Chinese (Simplified)
11
+ Classifier: Intended Audience :: Education
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: requires-python
20
+ Dynamic: summary
21
+
22
+ # cm_filekit
23
+
24
+ 《计算概论C》文件与目录任务工具库,适合文件读写和异常处理之后使用。
25
+
26
+ ```bash
27
+ pip install cm_filekit
28
+ ```
29
+
30
+ ```python
31
+ import cm_filekit
32
+
33
+ print(cm_filekit.count_by_extension('.'))
34
+ print(cm_filekit.find_large_files('.', min_mb=20))
35
+
36
+ report = cm_filekit.make_file_report('.')
37
+ print(report)
38
+
39
+ moves = cm_filekit.organize_by_extension('.', dry_run=True)
40
+ cm_filekit.write_csv('整理预览.csv', moves, header=['原文件', '新位置'])
41
+ ```
42
+
43
+ 这个库不替代 `open()`、循环和路径知识,而是把真实文件夹任务做得更容易上手。
@@ -0,0 +1,8 @@
1
+ README.md
2
+ setup.py
3
+ cm_filekit/__init__.py
4
+ cm_filekit/core.py
5
+ cm_filekit.egg-info/PKG-INFO
6
+ cm_filekit.egg-info/SOURCES.txt
7
+ cm_filekit.egg-info/dependency_links.txt
8
+ cm_filekit.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ cm_filekit
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,20 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name='cm_filekit',
5
+ version='1.0.0',
6
+ author='Chen Mo',
7
+ author_email='chenmoemail@126.com',
8
+ description='计算概论C课程文件夹扫描、统计、整理和安全读写工具',
9
+ long_description=open('README.md', encoding='utf-8').read(),
10
+ long_description_content_type='text/markdown',
11
+ packages=find_packages(),
12
+ python_requires='>=3.8',
13
+ classifiers=[
14
+ 'Programming Language :: Python :: 3',
15
+ 'License :: OSI Approved :: MIT License',
16
+ 'Operating System :: OS Independent',
17
+ 'Natural Language :: Chinese (Simplified)',
18
+ 'Intended Audience :: Education',
19
+ ],
20
+ )