Don't try reading or writing closed files

Accidentally trying to read or write files that are closed will cause a ValueError at runtime.

Python will raise "ValueError: I/O operation on closed file" if you try to read or write a file that has been closed.

Files opened via a with open(...) context manager will be automatically closed once the context manager is exited, meaning the file pointer, while still in scope, is no longer open to reads or writes.

If our GitHub code review bot spots this issue in your pull request it gives this advice:

code-review-doctorbotsuggested changes just now
catalogue.py
1
+
with open('some/path.txt') as f:
2
+
    line_one = f.readline()
3
+
4
+
line_two = f.readline()

Accidentally trying to read or write files that are closed will cause a ValueError at runtime.

Read more
Update catalogue.py

Instantly check if you have this issue for free

    Works with tools you use

    Read about how it works.