python给window电脑目录右键菜单增加批量读取子目录所有文本文件合并成单一文本输出与写入备份功能代码
代码语言:python
所属分类:其他
代码描述:python给window电脑目录右键菜单增加批量读取子目录所有文本文件合并成单一文本输出与写入备份功能代码,可以将目录下所有的文本代码文件以指定的分隔符规则分割合并成一个文本进行复制,然后直接发给ai聊天大模型,输入需求修改后,ai大模型以同样的分隔符但文本输出所有修改或新增文件,右键目录选择批量写入进行原目录文件备份后写入新的文件,适合完整项目文件源码想批量白嫖ai大模型的需求,这样不用api就能通过对话来白嫖ai大模型帮你写中大型项目源码。
代码标签: python window 电脑 目录 右键 菜单 增加 批量 读取 子目录 所有 文本 文件合 并
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
import os import sys import re import io import time import shutil from pathlib import Path, PurePosixPath import tkinter as tk from tkinter import ttk, messagebox, simpledialog, filedialog try: import winreg # only available on Windows except ImportError: winreg = None # ---------------- 配置(可根据需要微调) ---------------- # 认为是“代码文件”的扩展名(小写,包含点) CODE_EXTS = { ".py", ".pyw", ".ipynb", ".js", ".jsx", ".ts", ".tsx", ".java", ".kt", ".c", ".h", ".cpp", ".cxx", ".hpp", ".cc", ".cs", ".go", ".rb", ".php", ".rs", ".swift", ".m", ".mm", ".dart", ".lua", ".pl", ".r", ".sh", ".bat", ".ps1", ".sql", ".html", ".htm", ".css", ".scss", ".less", ".json", ".yaml", ".yml", ".xml", ".ini", ".toml", ".md", ".txt", ".vue", ".svelte", } # 忽略的目录名(小写,匹配目录名本身) IGNORE_DIRS = { ".git", ".svn", ".hg", "node_modules", "dist", "build", "out", "coverage", "__pycache__", ".venv", "venv", "env", ".idea", ".vscode", ".gradle", "target", "bin", "obj", ".next", ".cache", } # 忽略的文件名(小写,匹配文件名本身) IGNORE_FILES = { ".ds_store", "thumbs.db", "desktop.ini", } # 最大单文件大小(字节),超过则跳过(0 表示不限制) MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB,可按需调整 # ---------------- 工具函数 ---------------- def posix_rel_path(file_path: Path, base_dir: Path) -> str: rel = file_path.relative_to(base_dir) # 转换成 POSIX 正斜杠 return str(rel.as_posix()) def is_probably_binary(sample: bytes) -> bool: # 简单二进制判断:包含 NUL 字节,或高比例非文本字符 if b"\x00" in sample: return True # 允许常见换行与制表符 text_chars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100))) # 非文本比例 > 0.30 认为二进制(粗略) nontext = sum(c not in text_chars for c in sample) return (len(sample) > 0 and nontext / len(sample) > 0.30) def read_text_auto(path: Path) -> str: # 逐个编码尝试,适配中文 Windows 常见编码 encodings = ["utf-8", "utf-8-sig", "gb18030", "gbk", "cp936", "latin1"] data = None try: with open(path, "rb") as f: data = f.read() except Exception: return None if data is None: return None # 粗略跳过二进制 if is_probably_binary(data[:2048]): return None for enc in encodings: try: return data.decode(enc) except Exception: continue return None def within_dir(child: Path, base: Path) -> bool: try: child.resolve().relative_to(base.resolve()) return True except Exception: return False def collect_code_files(base_dir: Path): for root, dirs, files in os.walk(base_dir): # 过滤目录 dirs[:] = [d for d in dirs if d.lower() not in IGNORE_DIRS] for fn in files: if fn.lower() in IGNORE_FILES: continue fp = Path(root) / fn # 只要扩展名匹配 if fp.suffix.lower() in CODE_EXTS: # 大小限制 try: if MAX_FILE_SIZE and fp.stat().st_size > MAX_FILE_SIZE: .........完整代码请登录后点击上方下载按钮下载查看
网友评论0