Use run_shell with inline Python for reliable file I/O when execute_code_sandbox cannot access working directory files
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-run-shell-python-file-io-e7f8e16a35f8 ,按照其中的说明把「run-shell-python-file-io」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
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.
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.
Determine which files you need to read from or write to in the working directory.
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')
"
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
After execution, verify the files were created/modified correctly:
ls -la
cat output.txt
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.
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')
"
| 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 |