Python With Statement: files closed for you
with open(path) as f: closes the file when the block ends, even if an error happens inside it. Only files are shown.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-67-A: the file object is closed after the with block, also when an exception is raised inside it
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'hello\n' > in.txt
cat > s.py <<'EOS'
with open('in.txt') as f:
print(f.closed)
data = f.read()
print(f.closed)
print(repr(data))
try:
with open('in.txt') as g:
raise ValueError('boom')
except ValueError:
print(g.closed)
EOSCommand:
python3 s.pyRecorded output:
False
True
'hello\n'
TrueExit status: 0. The output matches the expected text written before the run.
TE-67-B: a file opened for writing in a with block is closed at the end of the block, and its text can be read back
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
with open('out.txt', 'w') as f:
f.write('x\n')
print(f.closed)
with open('out.txt') as g:
print(g.read().strip())
EOSCommand:
python3 s.pyRecorded output:
True
xExit status: 0. This status and the output match what was written before the run.
TE-67-C: expected non-zero exit: with open on a file that does not exist raises FileNotFoundError before the block starts
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
with open('nosuch.txt') as f:
print('inside')
EOSCommand:
python3 s.pyRecorded output: none, standard output was empty.
Standard error from the same run:
Traceback (most recent call last):
File "/tmp/b17-lbte9686/work/s.py", line 1, in <module>
with open('nosuch.txt') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'nosuch.txt'Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.
TE-67-B: the file was written inside a with block, f.closed printed True afterwards, and reading it back printed x. TE-67-C: open('nosuch.txt') raised before the block started, so inside was not printed. The program ended with exit status 1 and the last line of standard error is FileNotFoundError: [Errno 2] No such file or directory: 'nosuch.txt'.
Limits
Only files are shown. Other context managers were not tested.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-67-A | the file object is closed after the with block, also when an exception is raised inside it | 0 | PASS | 7c56d2737c77 | | TE-67-B | a file opened for writing in a with block is closed at the end of the block, and its text can be read back | 0 | PASS | 0cd6492bf5b7 | | TE-67-C | expected non-zero exit: with open on a file that does not exist raises FileNotFoundError before the block starts | 1 | PASS | c1385d24148c |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides