fallback-python-shell
它能做什么
这项技能在常用工具失效时,直接在 shell 中运行 Python 代码来读取文件并处理数据。它适合在常规工具出错时,需要处理文档、电子表格或数据的人。
适用场景
- ✓当代码沙箱在某项任务上返回未知错误的时候。
- ✓当文件读取器无法读取某种受支持格式的文件的时候。
- ✓当我需要读取并处理文本、JSON、CSV 或 Excel 文件的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/fallback-python-shell/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“文件读取器无法读取我的数据文件。请在 shell 中用 Python 读取它,并总结其内容。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会在 shell heredoc 中运行一段简短的 Python 脚本,它会打开文件、处理内容,并将结果打印回对话中。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Shell-Based Python Fallback
When to Use
Use this fallback pattern when:
execute_code_sandboxreturns 'unknown error'read_filereturns 'unknown error' for supported formats- You need to process documents, analyze data, or read files programmatically
Core Technique
Run Python code through run_shell using a heredoc. This bypasses sandbox execution issues while maintaining Python's full capabilities for file I/O and data processing.
Basic Pattern
python3 << 'EOF'
# Your Python code here
import json
import os
# Example: Read and process a file
with open('/path/to/file.txt', 'r') as f:
content = f.read()
print(content)
EOF
Key Syntax Points
- Use
python3 << 'EOF'(quoted EOF prevents variable expansion in the heredoc) - Indent Python code starting from column 0 (no extra indentation from shell)
- End with
EOFon its own line with no leading/trailing whitespace - Print results to stdout to capture them in the tool response
Common Use Cases
Reading Files (Fallback for read_file)
python3 << 'EOF'
import json
# Read text file
with open('document.txt', 'r') as f:
content = f.read()
print(content)
# Read JSON file
with open('data.json', 'r') as f:
data = json.load(f)
print(json.dumps(data, indent=2))
EOF
Processing Excel/CSV Files
python3 << 'EOF'
import pandas as pd
# Read Excel file
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
print(df.to_string())
print(f"Shape: {df.shape}")
# Read CSV
df = pd.read_csv('data.csv')
print(df.head(10))
EOF
Reading PDF Files
python3 << 'EOF'
import fitz # PyMuPDF
doc = fitz.open('document.pdf')
for page_num in range(len(doc)):
page = doc[page_num]
text = page.get_text()
print(f"=== Page {page_num + 1} ===")
print(text)
doc.close()
EOF
Reading Word Documents
python3 << 'EOF'
from docx import Document
doc = Document('document.docx')
for para in doc.paragraphs:
print(para.text)
EOF
Data Analysis
python3 << 'EOF'
import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
# Basic statistics
print(f"Shape: {df.shape}")
print(f"Columns: {list(df.columns)}")
print(df.describe())
# Filter and aggregate
result = df.groupby('category').agg({'value': 'sum'})
print(result)
EOF
Best Practices
Error Handling: Wrap file operations in try/except blocks
python3 << 'EOF' try: with open('file.txt', 'r') as f: content = f.read() print(content) except FileNotFoundError: print("ERROR: File not found") except Exception as e: print(f"ERROR: {e}") EOFLarge Output: For large files, process in chunks or print summaries
python3 << 'EOF' with open('large_file.csv', 'r') as f: for i, line in enumerate(f): if i < 10: print(line.strip()) else: print("... truncated ...") break EOFWorking Directory: Remember run_shell executes in the current working directory. Use absolute paths or ensure you're in the right directory.
Multiple Steps: Chain related operations in a single heredoc rather than multiple calls
python3 << 'EOF' # Do all related work in one call with open('input.json') as f: data = json.load(f) processed = [transform(x) for x in data] with open('output.json', 'w') as f: json.dump(processed, f) print("Processing complete") EOF
Troubleshooting
- Module not found: Some packages may not be available. Stick to standard library or commonly pre-installed packages (pandas, numpy are often available).
- Permission errors: Ensure files aren't in protected directories.
- Character encoding: Specify encoding explicitly:
open('file.txt', 'r', encoding='utf-8') - Very long code: Split into multiple heredocs or write to a temporary .py file first.
常见问题
什么是 heredoc?
这是一种通过 shell 命令一次性传入多行代码的方式。
文中提到了哪些文件类型?
指南提到纯文本、JSON,以及用 pandas 处理的 CSV 或 Excel 文件。
为什么要打印结果?
打印的输出会回到对话中,AI 便可以读取并汇报。
详情
作者的原始描述
Use run_shell with Python heredoc when execute_code_sandbox or read_file fail
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12883
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。