run-shell-python-file-io
Skill này làm gì
Skill này chỉ cho AI agent cách đọc, thay đổi và ghi file bằng Python chạy trong shell. Nó dành cho những người có sandbox không nhìn thấy các file trong thư mục làm việc của họ.
Khi nào nên dùng
- ✓Khi sandbox mã không truy cập được các file trong thư mục làm việc của bạn.
- ✓Khi bạn cần đọc một file, sửa nội dung và lưu kết quả.
- ✓Khi một công việc file dài cần một script Python tạm để chạy nhiều bước hơn.
Liên kết skill
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/run-shell-python-file-io/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Đọc file input.txt trong thư mục làm việc của tôi, chuyển văn bản thành chữ hoa và lưu thành output.txt. Sandbox không nhìn thấy thư mục này.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent chạy một chương trình Python ngắn trong shell, đọc file đầu vào, xử lý văn bản và ghi file đầu ra. Nó in thư mục làm việc và một thông báo hoàn tất để bạn xác nhận kết quả.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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 |
Câu hỏi thường gặp
Tại sao agent in thư mục làm việc?
Điều đó cho thấy script đang dùng thư mục nào, để bạn xác nhận các file nằm đúng chỗ.
Còn với các công việc dài hơn thì sao?
Skill gợi ý viết trước một file script Python tạm, rồi chạy nó khi logic phức tạp.
Nó có xóa file gốc không?
Ví dụ đọc file đầu vào và ghi một file đầu ra riêng, nên file đầu vào được giữ nguyên.
Chi tiết
Mô tả gốc của tác giả
Use run_shell with inline Python for reliable file I/O when execute_code_sandbox cannot access working directory files
repo: HKUDS/AgentPilot
Độ phổ biến
- Sao GitHub
- 7.8k
- Vị trí xếp hạng
- #12980
- Giấy phép
- MIT
- Cập nhật
- 1/11/2026
Bình luận
- …
Thêm skill từ kho mã này
agentpilot
7.8kGiúp agent của bạn cải thiện dự án từng bước bằng cách chuẩn bị tác vụ và chạy vòng lặp AgentPilot, gồm đề xuất, kiểm thử và chấm điểm thay đổi.
openspace
7.8kGiúp agent của bạn thiết lập OpenSpace, công cụ lưu và lấy lại skill của agent, rồi kết nối nó với ứng dụng AI của bạn qua MCP.
rsi-arena
7.8kGiúp agent của bạn thiết lập và chạy các bài kiểm thử benchmark AI chuẩn, như AIME và Terminal-Bench, để so sánh các phương pháp cải tiến khác nhau.
experiment-guide
7.8kGiúp agent của bạn lên kế hoạch các thí nghiệm nhỏ, đọc kết quả và ghi lại phát hiện khi tìm cách cải thiện một chương trình.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.