Codex 在 Windows 中文路径执行bash命令编码问题复盘

Codex 在 Windows 中文路径执行bash命令编码问题复盘

问题现象

在 Windows 环境下处理中文路径、中文文件名、中文 Markdown/PPT 等文件时,旧版 Windows PowerShell 中可能出现:

  • 中文文件名或路径在 Python 中变成 ????
  • Get-Content 或命令输出出现中文乱码
  • 明明文件存在,但脚本中 Path.exists() 返回 False
  • PPT、draw.io、Office、Python 脚本因路径被错误转码而找不到文件

脱敏示例:

text 复制代码
<项目目录>\ppt\<中文文件名>.pptx
<项目目录>\ppt\<中文文件名>v2.md

根因

主要原因是 Windows PowerShell 5.1 在中文内容经过 heredoc、管道、子进程参数传递时,编码边界不稳定。中文路径在进入 Python 或其他工具前可能已经被替换成 ????。这通常不是源文件损坏,而是 shell 编码/传参问题。

已验证环境

text 复制代码
Windows PowerShell: 5.1.26100.4768
PowerShell 7:      7.6.1
Python:            3.13.5
pwsh path:         C:\Program Files\PowerShell\7\pwsh.exe

PowerShell 7 当前编码:

text 复制代码
Console OutputEncoding: UTF-8
PowerShell OutputEncoding: UTF-8

Windows PowerShell 5.1 当前状态:

text 复制代码
Console OutputEncoding: UTF-8
PowerShell OutputEncoding: US-ASCII

解决方法

优先使用PowerShell 7:

powershell 复制代码
pwsh -NoLogo -NoProfile -Command "<command>"

检查:

powershell 复制代码
where.exe pwsh
pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion; [Console]::OutputEncoding.EncodingName; $OutputEncoding.EncodingName'

涉及中文路径时,即使在 PowerShell 7 下,也优先让 Python 从文件系统枚举真实路径:

python 复制代码
from pathlib import Path

p = next(Path("ppt").glob("*v2.md"))
text = p.read_text(encoding="utf-8")

避免在旧 PowerShell heredoc 或命令参数中直接硬写中文路径。

后续准则

  • 中文路径/中文文件优先用 pwsh,避免 Windows PowerShell 5.1。
  • 文本读写显式指定 encoding="utf-8"。
  • 中文文件名优先用 Path.glob() / Path.iterdir() 获取真实路径。
  • 遇到 ???? 时,先判断是终端编码/传参问题,不要直接改源文件。

来源

codex生成

相关推荐
AI大模型-小华2 天前
Codex 自动测试怎么用?从生成测试用例到修复失败用例
git·测试用例·ai编程·软件开发·codex·chatgpt pro·codex cli
全方位办公软件2 天前
Windows音频服务架构解析:AudioSrv与AudioEndpointBuilder排障
windows·命令行·powershell·音频服务·驱动修复·mmdevices·audiosrv
逐梦苍穹3 天前
不训练不微调:直接改权重矩阵,给小模型补上新能力
ai编程·codex·claudecode
无情的西瓜皮3 天前
eMudhra 根证书自 11-16 起失去微软产品默认信任:4 步 PowerShell 自查你的 Windows 是否已收到变更(Win11 实测)
windows·powershell
夕阳_醉了9 天前
Codex 客户端从装好到跑通:第三方 API 接入与配置避坑指南
人工智能·gpt·openai·codex·ai 编程
段一凡-华北理工大学11 天前
工业智能体:从Codex/Claude到项目实战~系列文章06:一张蓝图看懂“工业智能体×高炉炼铁“
人工智能·claude·codex·工业智能体·高炉炼铁智能化·高炉智能体技术栈
小年糕是糕手12 天前
【AI】用 AI 做自己的个人网站(上):选模板、写提示词,做出第一版
人工智能·app·网页开发·codex·index·styles·0基础教程
段一凡-华北理工大学14 天前
工业智能体:从Codex/Claude到项目实战~系列文章05:从“AI助手“到“工业智能体“:能力跃迁与落地范式
人工智能·python·claude·codex·工业智能体·高炉炼铁智能化
yuezhilangniao17 天前
win10中文 以及 PowerShell 开发日常
windows·powershell
孤狼GPT17 天前
ChatGPT、Codex数据库实战:MyBatis-Plus第一页查询正常,翻到第10000页为什么越来越慢?
mysql·chatgpt·mybatis-plus·codex·深分页