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
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -326,6 +326,27 @@ out
326
326
print(out)
327
327
```
328
328
329
+
We can also use `with` statement to contain operations on the file within a block.
330
+
331
+
Note that we used `a+` mode (standing for append+ mode) to allow appending new content at the end of the file and enable reading at the same time.
332
+
333
+
There are even [more modes](https://www.geeksforgeeks.org/reading-writing-text-files-python/) you could set when openning the file.
334
+
335
+
The trick is to check where the pointer is set: if the pointer is set at the end of the file `seek` method is needed to go back to the start of the file.
336
+
337
+
Here we set `file.seek(0)` to move the pointer back to the first line (line 0) of the file.
0 commit comments