Zum Inhalt springen
Skillpedia
Allgemeines Programmieren7.8k · #12980

run-shell-python-file-io

Was er macht

Dieser Skill zeigt dem KI-Agenten, wie er Dateien mit Python in der Shell liest, verändert und schreibt. Er richtet sich an Personen, deren Sandbox die Dateien in ihrem Arbeitsordner nicht sehen kann.

Wann du ihn nutzt

  • ✓Wenn die Code-Sandbox nicht auf Dateien in deinem Arbeitsordner zugreifen kann.
  • ✓Wenn du eine Datei lesen, ihren Text ändern und das Ergebnis speichern willst.
  • ✓Wenn ein längerer Dateijob ein temporäres Python-Skript für mehrere Schritte braucht.

Link zum Skill

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

Kopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.

So nutzt du ihn

Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.

Fertige Nachricht zum Einfügen

Nutze diesen Skill, um mir zu helfen: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/run-shell-python-file-io/SKILL.md Lies die SKILL.md unter diesem Link und folge ihren Anweisungen für meine Aufgabe.
Soll deine KI Skills wie diesen selbst finden?Installiere den kostenlosen Skillpedia-MCP: eine Zeile, und sie durchsucht vor jeder Aufgabe 15.000+ Skills.In 1 Minute installieren →

Beispielanfrage an deine KI

“Lies input.txt in meinem Arbeitsordner, wandle den Text in Großbuchstaben um und speichere ihn als output.txt. Die Sandbox sieht den Ordner nicht.”

5 neue Skills pro Woche. 1 Minute Lesezeit.

So funktioniert es

Der Agent führt in der Shell ein kurzes Python-Programm aus, das die Eingabedatei liest, den Text verarbeitet und die Ausgabedatei schreibt. Er gibt das Arbeitsverzeichnis und eine Abschlussmeldung aus, damit du das Ergebnis prüfen kannst.

Technischer Inhalt (originale SKILL.md)

Originaltext des Autors, auf Englisch, direkt von 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
Häufige Fragen

Warum gibt der Agent das Arbeitsverzeichnis aus?

So siehst du, welchen Ordner das Skript verwendet, und kannst prüfen, ob die Dateien dort liegen, wo du sie erwartest.

Was ist mit längeren Aufgaben?

Bei komplexer Logik schlägt der Skill vor, zuerst eine temporäre Python-Skriptdatei zu schreiben und sie dann auszuführen.

Löscht er die Originaldatei?

Das Beispiel liest die Eingabe und schreibt eine separate Ausgabedatei, die Eingabe bleibt also unverändert.

Details

Originalbeschreibung des Autors

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

repo: HKUDS/AgentPilot

Beliebtheit

GitHub-Sterne
7.8k
Platz im Ranking
#12980
Lizenz
MIT
Aktualisiert
1.11.2026

Ist dieser Skill nützlich?

0

Anmelden um abzustimmen.

Kommentare

  • …

Weitere Skills aus diesem Repository

Ähnliche Skills