diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py
index cfd6381..1d0c364 100644
--- a/pre_commit/staged_files_only.py
+++ b/pre_commit/staged_files_only.py
@@ -8,6 +8,7 @@ import time
 
 from pre_commit.util import CalledProcessError
 from pre_commit.util import cmd_output
+from pre_commit.util import mkdirp
 
 
 logger = logging.getLogger('pre_commit')
@@ -43,6 +44,7 @@ def staged_files_only(patch_dir):
             'Stashing unstaged files to {}.'.format(patch_filename),
         )
         # Save the current unstaged changes as a patch
+        mkdirp(patch_dir)
         with io.open(patch_filename, 'wb') as patch_file:
             patch_file.write(diff_stdout_binary)
 

Unstaged files + never ran pre-commit => "No such file or directory: .../.cache/pre-commit/patch..."
```
$ pre-commit run
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/asottile/.cache/pre-commit/patch1505686307.
An unexpected error has occurred: IOError: [Errno 2] No such file or directory: '/home/asottile/.cache/pre-commit/patch1505686307'
Check the log at /home/asottile/.cache/pre-commit/pre-commit.log
```

Stacktrace:

```python
Traceback (most recent call last):
  File "/home/asottile/workspace/pre-commit/pre_commit/error_handler.py", line 44, in error_handler
    yield
  File "/home/asottile/workspace/pre-commit/pre_commit/main.py", line 231, in main
    return run(runner, args)
  File "/home/asottile/workspace/pre-commit/pre_commit/commands/run.py", line 249, in run
    with ctx:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/home/asottile/workspace/pre-commit/pre_commit/staged_files_only.py", line 46, in staged_files_only
    with io.open(patch_filename, 'wb') as patch_file:
IOError: [Errno 2] No such file or directory: '/home/asottile/.cache/pre-commit/patch1505686307'
```
diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py
index aec55f5..36b1985 100644
--- a/tests/staged_files_only_test.py
+++ b/tests/staged_files_only_test.py
@@ -75,6 +75,15 @@ def test_foo_something_unstaged(foo_staged, patch_dir):
     _test_foo_state(foo_staged, 'herp\nderp\n', 'AM')
 
 
+def test_does_not_crash_patch_dir_does_not_exist(foo_staged, patch_dir):
+    with io.open(foo_staged.foo_filename, 'w') as foo_file:
+        foo_file.write('hello\nworld\n')
+
+    shutil.rmtree(patch_dir)
+    with staged_files_only(patch_dir):
+        pass
+
+
 def test_something_unstaged_ext_diff_tool(foo_staged, patch_dir, tmpdir):
     diff_tool = tmpdir.join('diff-tool.sh')
     diff_tool.write('#!/usr/bin/env bash\necho "$@"\n')

pip install -e .
parse_log_pytest
requirements.txt
3.6
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
aspy.yaml==1.3.0
attrs==22.2.0
cached-property==1.5.2
certifi==2021.5.30
coverage==6.2
distlib==0.3.9
filelock==3.4.1
flake8==5.0.4
identify==2.4.4
importlib-metadata==4.8.3
importlib-resources==5.4.0
iniconfig==1.1.1
mccabe==0.7.0
mock==5.2.0
nodeenv==1.6.0
packaging==21.3
platformdirs==2.4.0
pluggy==1.0.0
-e git+https://github.com/pre-commit/pre-commit.git@773a817f7fa300c5561e7d27ff6a67b11c261fc5#egg=pre_commit
py==1.11.0
pycodestyle==2.9.1
pyflakes==2.5.0
pyparsing==3.1.4
pytest==7.0.1
pytest-env==0.6.2
PyYAML==6.0.1
six==1.17.0
tomli==1.2.3
typing_extensions==4.1.1
virtualenv==20.17.1
zipp==3.6.0

name: pre-commit
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - aspy-yaml==1.3.0
      - attrs==22.2.0
      - cached-property==1.5.2
      - coverage==6.2
      - distlib==0.3.9
      - filelock==3.4.1
      - flake8==5.0.4
      - identify==2.4.4
      - importlib-metadata==4.8.3
      - importlib-resources==5.4.0
      - iniconfig==1.1.1
      - mccabe==0.7.0
      - mock==5.2.0
      - nodeenv==1.6.0
      - packaging==21.3
      - platformdirs==2.4.0
      - pluggy==1.0.0
      - py==1.11.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-env==0.6.2
      - pyyaml==6.0.1
      - six==1.17.0
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - virtualenv==20.17.1
      - zipp==3.6.0
prefix: /opt/conda/envs/pre-commit

tests/staged_files_only_test.py::test_does_not_crash_patch_dir_does_not_exist
tests/staged_files_only_test.py::test_foo_staged tests/staged_files_only_test.py::test_foo_nothing_unstaged tests/staged_files_only_test.py::test_foo_something_unstaged tests/staged_files_only_test.py::test_something_unstaged_ext_diff_tool tests/staged_files_only_test.py::test_foo_something_unstaged_diff_color_always tests/staged_files_only_test.py::test_foo_both_modify_non_conflicting tests/staged_files_only_test.py::test_foo_both_modify_conflicting tests/staged_files_only_test.py::test_img_staged tests/staged_files_only_test.py::test_img_nothing_unstaged tests/staged_files_only_test.py::test_img_something_unstaged tests/staged_files_only_test.py::test_img_conflict tests/staged_files_only_test.py::test_stage_utf8_changes tests/staged_files_only_test.py::test_stage_non_utf8_changes tests/staged_files_only_test.py::test_non_utf8_conflicting_diff tests/staged_files_only_test.py::test_crlf[true-True-True] tests/staged_files_only_test.py::test_crlf[true-True-False] tests/staged_files_only_test.py::test_crlf[true-False-True] tests/staged_files_only_test.py::test_crlf[true-False-False] tests/staged_files_only_test.py::test_crlf[false-True-True] tests/staged_files_only_test.py::test_crlf[false-True-False] tests/staged_files_only_test.py::test_crlf[false-False-True] tests/staged_files_only_test.py::test_crlf[false-False-False] tests/staged_files_only_test.py::test_crlf[input-True-True] tests/staged_files_only_test.py::test_crlf[input-True-False] tests/staged_files_only_test.py::test_crlf[input-False-True] tests/staged_files_only_test.py::test_crlf[input-False-False] tests/staged_files_only_test.py::test_whitespace_errors tests/staged_files_only_test.py::test_autocrlf_commited_crlf
pre_commit/staged_files_only.py
pre_commit/staged_files_only.py