# format-python-files

> Automatically format Python files after any Edit operation using black formatter. This hook runs 'black' on any .py file that Claude modifies, ensuring con

- Source: https://doutorai.pt/en/components/hooks__format-python-files
- Type: Automations
- Category: Post Tool
- Downloads: 677
- Site: Doutor AI (https://doutorai.pt/en) - AI tools for small businesses

An open-source community component, curated and adapted by Doutor AI.

## What it does and how to use it

A **hook** that automates post tool actions at key moments (before/after a tool runs, on save, etc.), without you having to think about it.

1. Install the hook (command above).
2. It runs automatically on the defined events.
3. Adjust the rules in the settings file if you want to fine-tune it.

## Install (Claude Code)

```bash
mkdir -p .claude/hooks && curl -fsSL https://doutorai.pt/data/lib/hooks__format-python-files.md -o .claude/hooks/format-python-files.json
```

Paste this command into the terminal, in your project folder, and the component installs automatically.

## Content

```
{
  "description": "Automatically format Python files after any Edit operation using black formatter. This hook runs 'black' on any .py file that Claude modifies, ensuring consistent Python code formatting. Requires black to be installed ('pip install black'). The command includes error suppression (2>/dev/null || true) so it won't fail if black is not installed.",
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit",
        "hooks": [
          {
            "type": "command",
            "command": "if [[ \"$CLAUDE_TOOL_FILE_PATH\" == *.py ]]; then black \"$CLAUDE_TOOL_FILE_P

- 🔒 Full content on the Pro plan (€7.99/month) -
```

## Related components

- [agents-md-loader](https://doutorai.pt/en/components/hooks__agents-md-loader): Automatically loads AGENTS.md configuration file content at session start to ensure Claude Code follows project-specific agent behavior. Onl
- [ai-bash-guard](https://doutorai.pt/en/components/hooks__ai-bash-guard): AI-powered bash command security guard. Before any Bash command runs, a lightweight Claude subagent evaluates it for destructive or irrevers
- [auto-git-add](https://doutorai.pt/en/components/hooks__auto-git-add): Automatically stage modified files with git add after editing. Helps maintain a clean git workflow by staging changes as they're made.
- [backup-before-edit](https://doutorai.pt/en/components/hooks__backup-before-edit): Create automatic backup of files before any Edit operation for safety. This hook creates a timestamped backup copy (filename.backup.timestam
- [build-on-change](https://doutorai.pt/en/components/hooks__build-on-change): Automatically trigger build processes when source files change. Detects common build tools and runs appropriate build commands.
- [change-logger](https://doutorai.pt/en/components/hooks__change-logger-2): Log every file mutation to CSV for demo prep. Records timestamp, tool, file path, action, and details for Edit, MultiEdit, Write, and Bash o

## Save hours every week with AI, no hassle

**Doutor AI** gives you 1,859 ready-to-copy AI prompts, by industry and in plain English - plus a complete technical library for those who want to go further. [See plans →](https://doutorai.pt/en/pricing)

---
Content by Doutor AI (https://doutorai.pt/en). When quoting, please cite the source: https://doutorai.pt/en/components/hooks__format-python-files
