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生成

相关推荐
搬砖的梦先生5 小时前
Codex 日志 Debug + 上下文 + 测试回归组合版
codex·ai辅助开发
kebeiovo1 天前
Windows下Codex安装详细配置使用指南
windows·codex
不懂的浪漫1 天前
Agent Knowledge Runtime:从一个 Codex 自动填工时案例看 Agent 工程化
ai·agent·codex
kaixuan_dashen2 天前
Codex使用DeepSeek API的方法(cc switch + codex bridge方案)
人工智能·codex·deepseek·cc switch·codex bridge
华科大胡子2 天前
Codex安装与VS Code联动
codex
搬砖的梦先生2 天前
Codex 用日志辅助 Debug 详解与操作指南
codex·ai辅助开发
资源分享助手2 天前
2026年最新版Codex客户端下载与安装教程
codex·codex客户端·codex客户端下载·codex客户端安装
vortex53 天前
PowerShell 的命令补全方案: PSReadLine + PSCompletions + argc + Carapace
windows·powershell
Jurio.3 天前
当 AI 不再只是对话:Codex app 的自动化功能
运维·人工智能·ai·自动化·codex