You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lectures/python_essentials.md
+58-10Lines changed: 58 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -326,8 +326,38 @@ out
326
326
print(out)
327
327
```
328
328
329
-
We can also use a `with` statement (also known as a [context manager](https://realpython.com/python-with-statement/#the-with-statement-approach)) which enables you to group operations on the file within a block which improves the clarity of your code.
329
+
We can also use a `with` statement to ensure the files are properly acquired
330
+
and released.
330
331
332
+
Containing the operations within the same block also improves the clarity of your code.
333
+
334
+
```{note}
335
+
This kind of block is formally referred to as a [*context*](https://realpython.com/python-with-statement/#the-with-statement-approach).
336
+
```
337
+
338
+
Let's try to convert the two examples above into a `with` statement.
339
+
340
+
We change the writing example first
341
+
```{code-cell} python3
342
+
343
+
with open('newfile.txt', 'w') as f:
344
+
f.write('Testing\n')
345
+
f.write('Testing again')
346
+
```
347
+
348
+
Note that we do not need to call the `close()` method since the `with` block
349
+
will ensure the stream is closed at the end of the block.
350
+
351
+
With slight modifications, we can also read files using `with`
352
+
353
+
```{code-cell} python3
354
+
with open('newfile.txt', 'r') as fo:
355
+
out = fo.read()
356
+
print(out)
357
+
```
358
+
Now suppose that we want to read input from one file and write output to another.
359
+
Here's how we could accomplish this task while correctly acquiring and returning
360
+
resources to the operating system using `with` statements:
331
361
332
362
```{code-cell} python3
333
363
with open("newfile.txt", "r") as f:
@@ -337,30 +367,48 @@ with open("newfile.txt", "r") as f:
337
367
fo.write(f'Line {i}: {line} \n')
338
368
```
339
369
370
+
The output file will be
371
+
340
372
```{code-cell} python3
341
-
fo = open('output.txt', 'r')
342
-
out = fo.read()
343
-
print(out)
373
+
with open('output.txt', 'r') as fo:
374
+
print(fo.read())
344
375
```
345
376
346
-
We can write the example above under the same context to reduce redundancy
377
+
We can simplify the example above by grouping the two `with` statements into one line
347
378
348
379
```{code-cell} python3
349
380
with open("newfile.txt", "r") as f, open("output2.txt", "w") as fo:
350
381
for i, line in enumerate(f):
351
382
fo.write(f'Line {i}: {line} \n')
352
383
```
353
384
385
+
The output file will be the same
386
+
354
387
```{code-cell} python3
355
-
fo = open('output2.txt', 'r')
356
-
out = fo.read()
357
-
print(out)
388
+
with open('output2.txt', 'r') as fo:
389
+
print(fo.read())
358
390
```
359
391
360
-
```{note}
361
-
Note that we only used `r` and `w` mode. There are [more modes](https://www.geeksforgeeks.org/reading-writing-text-files-python/) you could experiment with.
392
+
Suppose we want to continue to write into the existing file
393
+
instead of overwriting it.
394
+
395
+
we can switch the mode to `a` which stands for append mode
396
+
397
+
```{code-cell} python3
398
+
with open('output2.txt', 'a') as fo:
399
+
fo.write('\nThis is the end of the file')
400
+
```
401
+
402
+
```{code-cell} python3
403
+
with open('output2.txt', 'r') as fo:
404
+
print(fo.read())
362
405
```
363
406
407
+
```{note}
408
+
Note that we only covered `r`, `w`, and `a` mode here, which are the most commonly used modes.
409
+
Python provides [a variety of modes](https://www.geeksforgeeks.org/reading-writing-text-files-python/)
0 commit comments