You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IPython/Jupyter Notebook中停止列表的自动ANSI颜色格式化

解决Jupyter Notebook中ls命令返回ANSI颜色字符的问题

方法1:临时禁用ls的颜色输出

macOS采用BSD版本的ls,可通过以下两种方式临时关闭颜色格式:

  • 关闭颜色相关环境变量执行命令:
    alldirs = !CLICOLOR=0 ls -d ./3*[_-]12*/processing*/
    
  • 通过cat过滤ANSI颜色序列:
    alldirs = !ls -d ./3*[_-]12*/processing*/ | cat
    

方法2:全局禁用IPython shell命令的颜色输出

  1. 生成IPython配置文件(若未生成过):
    !ipython profile create
    
  2. 打开配置文件~/.ipython/profile_default/ipython_config.py,修改以下配置项:
    c.TerminalInteractiveShell.color_info = False
    c.ShellColors.color_scheme = 'NoColor'
    
    保存后重启Jupyter Notebook,后续所有!开头的shell命令都会默认输出无格式内容。

方法3:事后清理已获取路径中的ANSI字符

如果已经得到带ANSI字符的列表,可通过Python正则批量清理:

import re
ansi_pattern = re.compile(r'\x1b\[[0-9;]*m')
clean_alldirs = [ansi_pattern.sub('', path) for path in alldirs]

内容的提问来源于stack exchange,提问作者runmel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 20:05:39