# -*- coding: utf-8 -*- """生成「网络自检.bat」—— 纯 ASCII,彻底避开编码坑。 真机问题:双击后中文全是乱码。 根因:.bat 用 GBK 存盘 + chcp 936,但用户机器上控制台代码页并不是 936 (可能开了「Beta: 使用 UTF-8 提供全球语言支持」), 于是 bat 自己的中文和 Python 的输出都花了。 彻底方案: * **bat 里一个中文都不留**(纯 ASCII),只负责设 UTF-8 代码页并调用 Python * 中文全部交给 Python 输出,Python 自己把控制台设成 65001 * 脚本名用通配符找(*.py),这样中文文件名也不影响纯 ASCII 的 bat 这样无论系统默认代码页是什么都不会乱。 """ from __future__ import annotations import sys from pathlib import Path LINES = [ "@echo off", "chcp 65001 >nul 2>nul", "title LAN Remote - Network Self Check", 'cd /d "%~dp0"', "", "set \"PY=\"", "where python >nul 2>nul && set \"PY=python\"", 'if not defined PY where py >nul 2>nul && set "PY=py"', "if not defined PY goto NOPY", "", "rem Find the single .py next to this file (its name may be Chinese,", "rem which would break this pure-ASCII launcher if hard-coded).", 'set "SCRIPT="', 'for %%f in ("%~dp0*.py") do if not defined SCRIPT set "SCRIPT=%%~ff"', "if not defined SCRIPT goto NOFILE", "", "%PY% \"%SCRIPT%\"", "goto END", "", ":NOPY", "echo.", "echo [ERROR] Python not found (neither python nor py on PATH).", "echo.", "echo The self-check needs Python. Usually you do not need it -", "echo use the Toolbox instead:", "echo 1 Start agent", "echo 2 Fix firewall", "echo.", "goto END", "", ":NOFILE", "echo.", "echo [ERROR] No .py script found next to this .bat.", "echo.", "", ":END", "echo.", "pause", ] def main() -> int: target = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\tools\网络自检.bat") target.parent.mkdir(parents=True, exist_ok=True) with open(target, "w", encoding="ascii", newline="\r\n") as handle: handle.write("\n".join(LINES) + "\n") data = target.read_bytes() crlf = data.count(b"\r\n") lone = data.count(b"\n") - crlf try: data.decode("ascii") ascii_ok = True except UnicodeDecodeError: ascii_ok = False print(f" 路径: {target}") print(f" 大小: {len(data)} 字节 CRLF={crlf} 裸LF={lone} 纯ASCII={ascii_ok}") problems = [] if lone: problems.append("存在裸 LF") if not ascii_ok: problems.append("含非 ASCII 字符") if not any(b"*.py" in line for line in [data]): problems.append("没有用通配符找 .py") if problems: for item in problems: print(f" [失败] {item}") return 1 print(" 自检: 通过") return 0 if __name__ == "__main__": sys.exit(main())