跳到正文
Skillpedia
通用编程7.8k · #12980

run-shell-python-file-io

它能做什么

这项技能向 AI 智能体展示如何在 shell 中运行 Python,完成文件的读取、修改和写入。它适合沙箱无法看到工作文件夹中文件的人。

适用场景

  • ✓当代码沙箱无法访问你工作文件夹中的文件的时候。
  • ✓当你需要读取文件、修改其中的文字并保存结果的时候。
  • ✓当较长的文件任务需要用临时 Python 脚本执行更多步骤的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/run-shell-python-file-io/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/run-shell-python-file-io/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请读取工作文件夹中的 input.txt,把文字转换为大写,并保存为 output.txt。沙箱看不到这个文件夹。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会在 shell 中运行一段简短的 Python 程序,读取输入文件,处理其中的文字,并写入输出文件。它会打印工作目录和完成信息,方便你确认结果。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Run Shell Python File I/O

When to Use This Skill

Use this technique when execute_code_sandbox cannot access files in your working directory, but you need to perform file operations (read, write, transform) as part of your task execution.

The Pattern

Instead of using execute_code_sandbox, run Python code directly through run_shell with explicit path handling. This provides reliable file I/O capabilities within the task workspace.

How to Apply

Step 1: Identify File Access Needs

Determine which files you need to read from or write to in the working directory.

Step 2: Write Inline Python via run_shell

Construct a Python script that handles your file operations and execute it through run_shell:

python3 -c "
import os

# Get the working directory
work_dir = os.getcwd()
print(f'Working directory: {work_dir}')

# Read a file
with open('input.txt', 'r') as f:
    content = f.read()

# Process the content
processed = content.upper()

# Write to output file
with open('output.txt', 'w') as f:
    f.write(processed)

print('File operation complete')
"

Step 3: Handle Complex Logic

For more complex operations, write a temporary Python script file first:

cat > /tmp/process.py << 'EOF'
import os
import json

work_dir = os.getcwd()

# Read input
with open('data.json', 'r') as f:
    data = json.load(f)

# Transform
result = {k: v * 2 for k, v in data.items()}

# Write output
with open('result.json', 'w') as f:
    json.dump(result, f, indent=2)

print(f'Processed {len(data)} items')
EOF

python3 /tmp/process.py

Step 4: Verify File Operations

After execution, verify the files were created/modified correctly:

ls -la
cat output.txt

Best Practices

  1. Use Absolute or Relative Paths Explicitly: Always specify file paths clearly; os.getcwd() helps confirm your working directory.

  2. Handle Errors Gracefully: Add try/except blocks to catch file I/O errors:

    try:
        with open('file.txt', 'r') as f:
            content = f.read()
    except FileNotFoundError:
        print('File not found')
    
  3. Use Here-Docs for Complex Scripts: For scripts longer than a few lines, use bash here-documents to avoid escaping issues.

  4. Clean Up Temporary Files: If you create temporary scripts, consider removing them after execution.

  5. Check Working Directory First: Print os.getcwd() to confirm you're operating in the expected directory.

Common Use Cases

  • Reading configuration files from the workspace
  • Writing generated reports or outputs
  • Transforming data files (CSV, JSON, XML)
  • Batch processing multiple files
  • Generating structured documents (PDFs, spreadsheets)

Example: Processing Multiple Files

python3 -c "
import os
import glob

work_dir = os.getcwd()
files = glob.glob('*.txt')

for f in files:
    with open(f, 'r') as file:
        content = file.read()
    # Process each file
    with open(f'processed_{f}', 'w') as out:
        out.write(content.strip().upper())

print(f'Processed {len(files)} files')
"

Troubleshooting

Issue Solution
File not found Check os.getcwd() output; use absolute paths if needed
Permission denied Ensure the directory is writable; avoid protected system paths
Encoding errors Specify encoding explicitly: open('file.txt', 'r', encoding='utf-8')
Multi-line script issues Use here-doc (<< 'EOF') instead of -c for complex scripts
常见问题

为什么智能体要打印工作目录?

这样可以显示脚本正在使用哪个文件夹,你便能确认文件位于预期的位置。

较长的任务怎么办?

对于复杂的逻辑,这项技能建议先写一个临时的 Python 脚本文件,再运行它。

它会删除原始文件吗?

示例会读取输入并写入另一个输出文件,因此输入文件保持不变。

详情

作者的原始描述

Use run_shell with inline Python for reliable file I/O when execute_code_sandbox cannot access working directory files

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12980
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能