Agent skill

Python上下文管理器重定向Print输出到文件

使用Python的@contextmanager装饰器创建上下文管理器,拦截print输出,添加时间戳和自定义前缀后写入指定文件。

ECNU-ICALKgithub.com/ECNU-ICALKGitHub ↗
claude-code
Install
npx skills add ECNU-ICALK/AutoSkill --skill python上下文管理器重定向print输出到文件 --agent claude-code

Same command for any agent — swap --agent for codex, cursor, copilot.

Facts
Files in the skill folder: 1
SKILL.md size: 1 KB
Bundled scripts: none
Version: 0.1.0
Path: SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/python上下文管理器重定向print输出到文件/SKILL.md
Open the folder on GitHub →
Where it comes from
Stars: 539
Language: Python

Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.

From the SKILL.md

# Python上下文管理器重定向Print输出到文件 使用Python的@contextmanager装饰器创建上下文管理器,拦截print输出,添加时间戳和自定义前缀后写入指定文件。 ## Prompt # Role & Objective 你是一个Python编程助手。你的任务是实现一个上下文管理器,用于将print函数的输出重定向到文件,并自动添加时间戳和指定的前缀。 # Operational Rules & Constraints 1. 必须使用 `contextlib.contextmanager` 装饰器来实现上下文管理器。 2. 在上下文管理器内部,需要重定向 `sys.stdout` 到一个自定义的类或对象。 3. 自定义的写入逻辑必须包含: - 获取当前时间并格式化为字符串(例如:YYYY-MM-DD HH:MM:SS)。 - 在每条消息前添加用户指定的前缀(prefix)。 - 将带有时间戳和前缀的消息写入指定的文件路径(使用追加模式 'a')。 4. 必须使用 `try...finally` 结构确保在退出上下文时恢复原始的 `sys.stdout`。 5. 忽略仅包含空白字符的消息。 # Anti-Patterns - 不要使用类形式的上下文管理器(即不要手动实现 `__enter__` 和 `__exit__`),必须使用 `@contextmanager` 装饰器。 - 不要直接覆盖 `print` 函数,应通过重定向 `sys.stdout` 实现。 ## Triggers - python 重写print的内容都写到文件中 并指定一些log的prefix - 把它改成上下文管理器的实现方式 - 使用contextmanager重定向print输出 - python print重定向到文件带前缀

What's inside
Steps it walks through
  1. Prompt
  2. Triggers
More from AutoSkill
All skills →
About this skill
What does the Python上下文管理器重定向Print输出到文件 skill do?

使用Python的@contextmanager装饰器创建上下文管理器,拦截print输出,添加时间戳和自定义前缀后写入指定文件。

How do I install it?

Run `npx skills add ECNU-ICALK/AutoSkill --skill python上下文管理器重定向print输出到文件 --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.

Where does this skill come from?

From ECNU-ICALK/AutoSkill, a repository with 539 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.

Is a popular skill a good skill?

Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.

Keep going