Langsung ke konten
Skillpedia
Pemrograman umum7.8k Β· #13058

large-file-write-mkdir

Fungsinya

Skill ini memperluas cadangan file besar dengan memastikan folder tujuan ada sebelum penulisan. Skill ini ditujukan untuk orang yang menyimpan file panjang ke folder yang mungkin belum ada.

Kapan dipakai

  • βœ“Saat alat tulis biasa gagal pada file besar.
  • βœ“Saat folder untuk file baru belum ada.
  • βœ“Saat penulisan Python gagal dengan FileNotFoundError karena folder hilang.

Link skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/large-file-write-mkdir/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/large-file-write-mkdir/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œSimpan laporan panjang ini ke subfolder reports yang baru. Penulisan sebelumnya gagal, jadi buat foldernya lalu tulis filenya.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Agent menjalankan mkdir -p pada folder induk, yang membuat folder yang hilang, lalu menjalankan heredoc Python untuk menulis file. Jika folder induk tidak diketahui, agent dapat mencarinya dengan dirname terlebih dahulu.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Large File Write Fallback (with Directory Pre-creation)

Problem

When writing large files (typically several KB or more), two common tools may fail with unknown error due to internal payload size limits:

  • write_file β€” has a maximum content size it can handle in a single call.
  • shell_agent β€” may also hit payload limits when the task description includes large inline content.

A second class of failure occurs when the target directory does not exist: Python's open() will raise FileNotFoundError if any intermediate directory is missing. Always pre-create the parent directory to prevent this.

Solution

Use run_shell with a mkdir -p prefix and a Python heredoc pattern. The mkdir -p ensures the target directory exists; the heredoc streams the file content through stdin directly into Python's open(), bypassing the payload constraints of the other tools.

Template

mkdir -p "<PARENT DIRECTORY OF TARGET PATH>" && python3 - << 'EOF'
content = """<FILE CONTENTS HERE>"""
with open("<TARGET PATH>", "w") as f:
    f.write(content)
EOF

Pass this as the command parameter to run_shell.

Tip: If the parent directory is not known in advance, use the shell dirname helper: mkdir -p "$(dirname '<TARGET PATH>')" && python3 - << 'EOF'

Step-by-Step Instructions

  1. Attempt the normal write using write_file first. If it succeeds, you are done.

  2. If write_file fails (especially with unknown error or a timeout on large content), do NOT retry with shell_agent using inline content β€” it will likely fail for the same reason.

  3. Use the run_shell heredoc fallback:

    • Pre-create the parent directory with mkdir -p <parent_dir>. This is a no-op when the directory already exists, so it is always safe to include. Skipping this step causes Python's open() to raise FileNotFoundError for any new or deeply-nested path.
    • Embed the full file content inside a Python triple-quoted string.
    • Specify the target path inside the open() call.
    • Pass the entire block (mkdir + heredoc) as the command to run_shell.
  4. Escape carefully inside the heredoc:

    • Backslashes that should be literal in the file must be doubled (\\).
    • Triple-quotes inside the content must be escaped (\"\"\").
    • The heredoc delimiter EOF must not appear on a line by itself inside the content (rename it to PYEOF or FILEEOF if needed).
  5. Verify the write by following up with a run_shell call such as:

    wc -l <TARGET PATH> && head -5 <TARGET PATH>
    

Full Example

Suppose you need to write a large TypeScript file to src/components/Dashboard.ts:

mkdir -p "src/components" && python3 - << 'PYEOF'
content = """import { foo } from './foo';

export interface DashboardData {
  title: string;
  items: string[];
}

export function createDashboard(data: DashboardData): string {
  return `<div>${data.title}</div>`;
}
"""
with open("src/components/Dashboard.ts", "w") as f:
    f.write(content)
PYEOF

Pass the above (without the surrounding code fence) as the command argument to run_shell.

When to Use This Pattern

Situation Recommended tool
Small file (< ~2 KB) write_file
Medium file, no errors yet write_file (try first)
Large file or write_file failed run_shell + mkdir -p + Python heredoc
shell_agent also fails on large inline content run_shell + mkdir -p + Python heredoc
Target directory may not exist Always add mkdir -p <parent_dir> first

Notes

  • This pattern works for any text-based file (TypeScript, Python, JSON, YAML, Markdown, etc.).
  • For binary files, adapt the approach to use base64 decoding inside the Python script.
  • The heredoc delimiter (EOF, PYEOF, FILEEOF) can be any string not present as a standalone line in your content β€” choose accordingly.
  • This technique is also useful when content contains characters that would need heavy shell escaping in a plain echo or printf approach.
  • The mkdir -p command is idempotent: it succeeds whether or not the directory already exists, so it is always safe to include.
  • To derive the parent directory from a target path in shell: mkdir -p "$(dirname '<TARGET PATH>')" works as a generic alternative when the parent path is not already known.
Pertanyaan umum

Mengapa menjalankan mkdir dulu?

Python akan gagal dengan FileNotFoundError jika ada bagian dari path folder tujuan yang hilang.

Apa yang dilakukan mkdir -p?

Flag -p membuat semua folder induk yang hilang dalam satu perintah, dan tidak melakukan apa-apa jika folder sudah ada.

Bagaimana jika saya tidak tahu foldernya?

Skill ini menunjukkan penggunaan dirname pada path tujuan untuk menemukan folder induk.

Detail

Deskripsi asli dari pembuat

Reliable fallback technique for writing large file contents when write_file and shell_agent both fail with 'unknown error' due to payload size limits β€” uses run_shell with a Python heredoc to bypass tool constraints, with an explicit mkdir -p step to ensure the target directory exists before writing.

repo: HKUDS/AgentPilot

Popularitas

Bintang GitHub
7.8k
Peringkat
#13058
Lisensi
MIT
Diperbarui
1/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa