# How to Properly Close a PDF with PyMuPDF

`doc.close()` closes a PDF document that you previously opened with PyMuPDF.

## What does `doc.close()` do?

`doc.close()`:

*   Releases resources and memory associated with the document.
    
*   Closes the document and its underlying resources.
    
*   Makes the Document object unavailable for further document operations.
    

## When should you use it?

*   Use `doc.close()` when you are finished working with a PDF, especially when:
    
*   You created or modified a PDF.
    
*   You are processing many PDFs in a loop.
    
*   You want to make sure the document is no longer being held open.
    

**Important:** After calling `doc.close()`, you can no longer work with the document. Attempting to perform operations on a closed document can raise: `ValueError: document closed`

If you need to work with the PDF again, you can open it with `pymupdf.open()`:

```python
doc = pymupdf.open("sample.pdf")
```

This creates a new Document object that you can work with normally.

## Should you save the PDF document before closing it?

If you've modified the PDF, make sure you save your changes to a new file before closing the document.

For example:

```python
doc.save("modified.pdf")
```

If you close a modified document without saving it, your changes may be lost.

`doc.close()` *does not save your changes.*

* * *

## Open the notebook

Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.

\[[Open in Google Colab](https://colab.research.google.com/drive/1j8jc-bC_2Fcs4IVPnzfPDA3ZiFHjqirj?usp=sharing)\]

* * *

## More PDF Python Resources

Want to learn how to open a PDF file?

Check out this mini-guide:

[Open Your First PDF with PyMuPDF](https://payhip.com/PDFPythonHub/blog/news/open-your-first-pdf-with-pymupdf)
