Skip to content

Commit bb1d556

Browse files
committed
Add explanations for the with statement
1 parent 1e83015 commit bb1d556

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

notebooks/beginner/file_io.ipynb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@
7373
" print(line.strip())"
7474
]
7575
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"The [`with`](https://docs.python.org/3/reference/compound_stmts.html#the-with-statement) statement is for obtaining a [context manager](https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers) that will be used as an execution context for the commands inside the `with`. Context managers guarantee that certain operations are done when exiting the context. \n",
81+
"\n",
82+
"In this case, the context manager guarantees that `simple_file.close()` is implicitly called when exiting the context. This is a way to make developers life easier: you don't have to remember to explicitly close the file you openened nor be worried about an exception occuring while the file is open. Unclosed file maybe a source of a resource leak. Thus, prefer using `with open()` structure always with file I/O.\n",
83+
"\n",
84+
"To have an example, the same as above without the `with`."
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"file_path = os.path.join(data_dir, 'simple_file.txt')\n",
94+
"\n",
95+
"# THIS IS NOT THE PREFERRED WAY\n",
96+
"simple_file = open(file_path, 'r')\n",
97+
"for line in simple_file:\n",
98+
" print(line.strip())\n",
99+
"simple_file.close() # This has to be called explicitly "
100+
]
101+
},
76102
{
77103
"cell_type": "markdown",
78104
"metadata": {},

notebooks/beginner/html/file_io.html

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11777,7 +11777,7 @@
1177711777
}
1177811778

1177911779
$( document ).ready(function(){
11780-
show=false;
11780+
show=false;
1178111781
$('div.output').hide()
1178211782
});
1178311783
</script>
@@ -11949,13 +11949,64 @@ <h2 id="Reading-files">Reading files<a class="anchor-link" href="#Reading-files"
1194911949
</div>
1195011950
<div class="inner_cell">
1195111951
<div class="text_cell_render border-box-sizing rendered_html">
11952-
<h2 id="Writing-files">Writing files<a class="anchor-link" href="#Writing-files">&#182;</a></h2>
11952+
<p>The <a href="https://docs.python.org/3/reference/compound_stmts.html#the-with-statement"><code>with</code></a> statement is for obtaining a <a href="https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers">context manager</a> that will be used as an execution context for the commands inside the <code>with</code>. Context managers guarantee that certain operations are done when exiting the context.</p>
11953+
<p>In this case, the context manager guarantees that <code>simple_file.close()</code> is implicitly called when exiting the context. This is a way to make developers life easier: you don't have to remember to explicitly close the file you openened nor be worried about an exception occuring while the file is open. Unclosed file maybe a source of a resource leak. Thus, prefer using <code>with open()</code> structure always with file I/O.</p>
11954+
<p>To have an example, the same as above without the <code>with</code>.</p>
11955+
1195311956
</div>
1195411957
</div>
1195511958
</div>
1195611959
<div class="cell border-box-sizing code_cell rendered">
1195711960
<div class="input">
1195811961
<div class="prompt input_prompt">In&nbsp;[4]:</div>
11962+
<div class="inner_cell">
11963+
<div class="input_area">
11964+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">file_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">data_dir</span><span class="p">,</span> <span class="s1">&#39;simple_file.txt&#39;</span><span class="p">)</span>
11965+
11966+
<span class="c1"># THIS IS NOT THE PREFERRED WAY</span>
11967+
<span class="n">simple_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
11968+
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">simple_file</span><span class="p">:</span>
11969+
<span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
11970+
<span class="n">simple_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="c1"># This has to be called explicitly </span>
11971+
</pre></div>
11972+
11973+
</div>
11974+
</div>
11975+
</div>
11976+
11977+
<div class="output_wrapper">
11978+
<div class="output">
11979+
11980+
11981+
<div class="output_area">
11982+
11983+
<div class="prompt"></div>
11984+
11985+
11986+
<div class="output_subarea output_stream output_stdout output_text">
11987+
<pre>First line
11988+
Second line
11989+
Third
11990+
And so the story goes!
11991+
</pre>
11992+
</div>
11993+
</div>
11994+
11995+
</div>
11996+
</div>
11997+
11998+
</div>
11999+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
12000+
</div>
12001+
<div class="inner_cell">
12002+
<div class="text_cell_render border-box-sizing rendered_html">
12003+
<h2 id="Writing-files">Writing files<a class="anchor-link" href="#Writing-files">&#182;</a></h2>
12004+
</div>
12005+
</div>
12006+
</div>
12007+
<div class="cell border-box-sizing code_cell rendered">
12008+
<div class="input">
12009+
<div class="prompt input_prompt">In&nbsp;[5]:</div>
1195912010
<div class="inner_cell">
1196012011
<div class="input_area">
1196112012
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">new_file_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">data_dir</span><span class="p">,</span> <span class="s1">&#39;new_file.txt&#39;</span><span class="p">)</span>
@@ -11980,7 +12031,7 @@ <h2 id="Writing-files">Writing files<a class="anchor-link" href="#Writing-files"
1198012031
</div>
1198112032
<div class="cell border-box-sizing code_cell rendered">
1198212033
<div class="input">
11983-
<div class="prompt input_prompt">In&nbsp;[5]:</div>
12034+
<div class="prompt input_prompt">In&nbsp;[6]:</div>
1198412035
<div class="inner_cell">
1198512036
<div class="input_area">
1198612037
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">new_file_path</span><span class="p">):</span> <span class="c1"># make sure it&#39;s there</span>

0 commit comments

Comments
 (0)