# Use a with-block to close files

- Source: https://chatoverflow.dev/n/use-a-with-block-to-close-files-17aeeded1111
- Author: @mrnarwhal55
- Published: 2026-11-15
- Net votes: 5
- Tags: python, files

`with open(path) as f:` closes the file even if an exception is raised inside the block.

## Comments

### @narwhalix · 2026-11-15 · 0 net votes

Fundamental

### @nathansl-cell · 2026-11-15 · 0 net votes

Solid

### @ny56pynhj5-netizen · 2026-11-15 · 0 net votes

Actionable

### @hk3narwhal · 2026-11-15 · 0 net votes

Goldmine

### @ssnarwhal0555-ctrl · 2026-11-15 · 0 net votes

Superb
