본문으로 건너뛰기
Skillpedia
일반 코딩7.8k · #13058

large-file-write-mkdir

하는 일

이 스킬은 쓰기 전에 대상 폴더가 존재하도록 하여 큰 파일 대체 방법을 확장합니다. 아직 없는 폴더에 긴 파일을 저장하는 사람을 위한 스킬입니다.

사용하는 경우

  • ✓일반 쓰기 도구가 큰 파일에서 실패할 때.
  • ✓새 파일을 둘 폴더가 아직 없을 때.
  • ✓Python 쓰기가 폴더가 없어 FileNotFoundError로 실패할 때.

스킬 링크

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

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/large-file-write-mkdir/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“이 긴 보고서를 새 reports 하위 폴더에 저장해 주세요. 전에 쓰기가 실패했으니 폴더를 만들고 파일을 써 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

에이전트는 상위 폴더에 mkdir -p를 실행해 없는 폴더를 모두 만든 다음, Python heredoc으로 파일을 씁니다. 상위 폴더가 무엇인지 모르면 dirname으로 찾을 수 있습니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 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.
자주 묻는 질문

mkdir을 왜 먼저 실행하나요?

대상 폴더 경로의 어느 부분이라도 없으면 Python이 FileNotFoundError로 실패하기 때문입니다.

mkdir -p는 무엇을 하나요?

-p 플래그는 없는 상위 폴더를 한 명령으로 모두 만들며, 이미 있으면 아무 일도 하지 않습니다.

폴더를 모르면 어떻게 하나요?

대상 경로에 dirname을 써서 상위 폴더를 찾는 방법을 스킬이 보여 줍니다.

상세 정보

작성자의 원본 설명

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

인기도

GitHub 스타
7.8k
순위
#13058
라이선스
MIT
업데이트
2026. 11. 1.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬