run-shell-python-file-io
できること
このスキルは、AIエージェントがシェルで実行する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に貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“私のワーキングフォルダーにあるinput.txtを読み込み、テキストを大文字に変換して、output.txtとして保存してください。サンドボックスからはこのフォルダーが見えません。”
毎週5つの新しいスキル。1分で読めます。
仕組み
エージェントはシェルで短い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
Use Absolute or Relative Paths Explicitly: Always specify file paths clearly;
os.getcwd()helps confirm your working directory.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')Use Here-Docs for Complex Scripts: For scripts longer than a few lines, use bash here-documents to avoid escaping issues.
Clean Up Temporary Files: If you create temporary scripts, consider removing them after execution.
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
コメント
- …
このリポジトリの他のスキル
agentpilot
7.8kエージェントが、作業を準備し、変更案を提案してテストし、評価するAgentPilotのループを実行することで、プロジェクトを段階的に改善できるよう支援します。
openspace
7.8kエージェントが、スキルを保存し取り出すツールであるOpenSpaceをセットアップし、MCPを通じてAIアプリに接続できるよう支援します。
rsi-arena
7.8kエージェントが、AIMEやTerminal-Benchなどの標準的なAIベンチマークテストをセットアップして実行し、異なる改善方法を比較できるよう支援します。
experiment-guide
7.8kエージェントが、プログラムを改善する方法を調べる間に、小さな実験を計画し、その結果を読み、発見を記録できるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。