Python Lab 12: Text File Handling with Python

Lab12: Text File Handling with Python

<div class="whitepaper-body-page">

<h4 class="wp-block-heading">Read Mode Functions</h4>



<p>To open a file in read mode, we need to use the following function:</p>



<ul><li><code>open(&quot;filename&quot;, &quot;r&quot;)</code> function opens a file in read mode. Both the file path and the mode need to be mentioned in the arguments.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile = </span><span class="enlighter-m0">open</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;RSTForum.txt&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&quot;r&quot;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div></div><div class="enlighter-raw">myfile = open( &#39;RSTForum.txt&#39;, &quot;r&quot; ) </div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile = open( &#39;RSTForum.txt&#39;, &quot;r&quot; ) </pre>



<p>To read the content of the opened file, we need to use the read() function. Following are different read functions:</p>



<ul><li><code>read()</code> function reads the entire file</li><li><code>seek(0)</code> function sets the cursor to a particular position it starts to read from, here it starts reading from 0th character</li><li><code>read(5)</code> function reads 5 characters from the content</li><li><code>readline()</code> function reads one line of the content</li><li><code>readlines()</code> function reads all the lines in the content and returns a list of all the lines.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">read</span><span class="enlighter-g1">()</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">seek</span><span class="enlighter-g1">(</span><span class="enlighter-n1">0</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">read</span><span class="enlighter-g1">(</span><span class="enlighter-n1">5</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">readline</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">readlines</span><span class="enlighter-g1">()</span><span class="enlighter-text"> </span></div></div></div><div class="enlighter-raw">myfile.read() 
myfile.seek(0) 
myfile.read(5) 
myfile.readline()
myfile.readlines() </div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile.read() 
myfile.seek(0) 
myfile.read(5) 
myfile.readline()
myfile.readlines() </pre>



<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-11122" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-11122" aria-expanded="false">Show Output</button><div id="ac-11122" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>&#39;RST Forum\nThis is line 2\nThis is line 3\nThis is line 4\nThis is the last line &#39;
0
&#39;RST F&#39;
&#39;RST Forum\n&#39;
[&#39;RST Forum\n&#39;, &#39;This is line 2\n&#39;, &#39;This is line 3\n&#39;, &#39;This is line 4\n&#39;, &#39;This is the last line &#39;]</code></pre>



<p>    </p>
</div></div>



<p>  To save and close the file content using python, we use the following function:</p>



<ul><li><code>close()</code> function saves the content in the text file and releases content of the file and the variable pointing to it from the memory location.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">close</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">myfile.close()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile.close()</pre>



<h4 class="wp-block-heading">Write Mode Functions</h4>



<p>To open a file in write mode, we use the following function:</p>



<ul><li> <code>open(&quot;filename&quot;, &#39;w&#39;)</code> function creates a<span class="has-inline-color has-vivid-red-color"> new </span>file and opens it in write mode. We cannot read the content of the file in write mode. We can only write data in write mode. If the file exists, it will overwrite the file and existing data will be lost.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile = </span><span class="enlighter-m0">open</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;vendors.txt&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;w&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">myfile = open( &#39;vendors.txt&#39;, &#39;w&#39; )</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile = open( &#39;vendors.txt&#39;, &#39;w&#39; )</pre>



<p>To write data in the new text file, we use the following functions:</p>



<ul><li><code>write()</code> function writes one line in the file.</li><li><code>writelines()</code> function writes multiple lines in the file and needs to be passed a list of lines.</li><li><code>read()</code> function is not supported in write mode</li><li><code>close()</code> function saves the  content in the <span class="has-inline-color has-vivid-red-color">new </span>text file and releases content of the file and the variable pointing to it from the memory location.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">write</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;Apple \n&#39;</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">writelines</span><span class="enlighter-g1">([</span><span class="enlighter-s0">&#39;Dell\n&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;MSI\n&#39;</span><span class="enlighter-g1">])</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">read</span><span class="enlighter-g1">()</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">close</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">myfile.write(&#39;Apple \n&#39;)
myfile.writelines([&#39;Dell\n&#39;, &#39;MSI\n&#39;])
myfile.read()
myfile.close()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile.write(&#39;Apple \n&#39;)
myfile.writelines([&#39;Dell\n&#39;, &#39;MSI\n&#39;])
myfile.read()
myfile.close()</pre>



<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-18308" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-18308" aria-expanded="false">Show Output</button><div id="ac-18308" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Apple
Dell
MSI
Traceback (most recent call last):
File &quot;&quot;, line 1, in module &lt;io.UnsupportedOperation&gt;: not readable</code></pre>
</div></div>



<h4 class="wp-block-heading">Append Mode Functions</h4>



<p>To open a file in the append mode, the following function is used:</p>



<ul><li><code> open(&quot;filename&quot;, &#39;a&#39;)</code> function opens an existing file and can perform write operations in the existing file. We cannot read the contents of the existing file in this mode. We can only add new data in the file. Append mode does not overwrite the file and existing data will not be lost.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile = </span><span class="enlighter-m0">open</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;vendors.txt&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;a&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">myfile = open( &#39;vendors.txt&#39;, &#39;a&#39; )</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile = open( &#39;vendors.txt&#39;, &#39;a&#39; )</pre>



<p>To write data in the existing file, the following functions are used</p>



<ul><li><code>write()</code> function appends data at the last line of the file</li><li><code>writelines() </code>function appends multiple lines at the end of the file</li><li><code>seek(0)</code> function sets the cursor to the 0th character of the file</li><li><code>read()</code> function is not supported in append mode</li><li><code>close()</code> function saves the content in the existing text file and releases content of the file and the variable pointing to it from the memory location.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">write</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;Lenovo \n&#39;</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">writelines</span><span class="enlighter-g1">([</span><span class="enlighter-s0">&#39;Microsoft\n&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;HP\n&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;Toshiba\n&#39;</span><span class="enlighter-g1">]</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">seek</span><span class="enlighter-g1">(</span><span class="enlighter-n1">0</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">read</span><span class="enlighter-g1">(</span><span class="enlighter-n1">5</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">close</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">myfile.write(&#39;Lenovo \n&#39;)
myfile.writelines([&#39;Microsoft\n&#39;, &#39;HP\n&#39;, &#39;Toshiba\n&#39;]
myfile.seek(0)
myfile.read(5)
myfile.close()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile.write(&#39;Lenovo \n&#39;)
myfile.writelines([&#39;Microsoft\n&#39;, &#39;HP\n&#39;, &#39;Toshiba\n&#39;]
myfile.seek(0)
myfile.read(5)
myfile.close()</pre>



<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-67753" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-67753" aria-expanded="false">Show Output</button><div id="ac-67753" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>8
0
Traceback (most recent call last):
File &quot;&quot;, line 1, in &lt;module&gt;
io.UnsupportedOperation: not readable</code></pre>
</div></div>



<h4 class="wp-block-heading">Write Plus Mode Functions</h4>



<p>To open a file in write-plus mode, the following function is used</p>



<ul><li><code> open(&quot;filename&quot;, &#39;w+&#39;)</code> opens a new file in write-plus mode. We can read the content of the file in write-plus mode. If the file exists, it will overwrite the file and existing data will be lost.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile = </span><span class="enlighter-m0">open</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;vendors.txt&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&quot;w+&quot;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">myfile = open( &#39;vendors.txt&#39;, &quot;w+&quot; )</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile = open( &#39;vendors.txt&#39;, &quot;w+&quot; )</pre>



<p>To use write-plus functions, the following functions are used</p>



<ul><li><code>write()</code> function writes one line in the new file</li><li><code>seek(0) </code>function sets the cursor to the 0th character of the file</li><li><code>read()</code> function reads the entire file and is supported in write-plus mode</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">write</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;Razer Blade \n&#39;</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">seek</span><span class="enlighter-g1">(</span><span class="enlighter-n1">0</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">read</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">myfile.write(&#39;Razer Blade \n&#39;)
myfile.seek(0)
myfile.read()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile.write(&#39;Razer Blade \n&#39;)
myfile.seek(0)
myfile.read()</pre>



<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-30296" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-30296" aria-expanded="false">Show Output</button><div id="ac-30296" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>13
0
&#39;Razer Blade \n&#39;</code></pre>
</div></div>



<h4 class="wp-block-heading">Append Plus Mode Functions</h4>



<p>To open a file in the append-plus mode, the following function is used</p>



<ul><li><code> open(&quot;filename&quot;, &#39;a+&#39;)</code> function opens an existing file and can perform write operations in the existing file. We can read the contents of the existing file in this mode. Append mode does not overwrite the file and existing data will not be lost.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile = </span><span class="enlighter-m0">open</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;vendors.txt&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;a+&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">myfile = open( &#39;vendors.txt&#39;, &#39;a+&#39; )</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile = open( &#39;vendors.txt&#39;, &#39;a+&#39; )</pre>



<p>To write data in the existing file, the following functions are used</p>



<ul><li><code>write()</code> function appends one line at the end of the file</li><li><code>seek(0) </code>function sets the cursor to 0th position</li><li><code>read()</code> function reads the entire file</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">write</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;Amazon \n&#39;</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">seek</span><span class="enlighter-g1">(</span><span class="enlighter-n1">0</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">myfile.</span><span class="enlighter-m1">read</span><span class="enlighter-g1">()</span></div></div></div><div class="enlighter-raw">myfile.write(&#39;Amazon \n&#39;)
myfile.seek(0)
myfile.read()</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">myfile.write(&#39;Amazon \n&#39;)
myfile.seek(0)
myfile.read()</pre>



<p>Another way of opening a file:</p>



<ul><li><code>with</code> statement is used in python to make code more readable and shorter</li><li>All the commands are indented within the with statement</li><li>We don’t need to close the file explicitly by using with statement, it closes automatically after coming out of the identation.</li></ul>



<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">with</span><span class="enlighter-text"> </span><span class="enlighter-m0">open</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;vendors.txt&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&quot;a+&quot;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span><span class="enlighter-k0">as</span><span class="enlighter-text"> myfile:</span></div></div><div class=""><div><span class="enlighter-text">    myfile.</span><span class="enlighter-m1">write</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;Acer \n&#39;</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">    myfile.</span><span class="enlighter-m1">write</span><span class="enlighter-g1">(</span><span class="enlighter-s0">&#39;Asus \n&#39;</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">with open(&#39;vendors.txt&#39;, &quot;a+&quot; ) as myfile:
    myfile.write(&#39;Acer \n&#39;)
    myfile.write(&#39;Asus \n&#39;)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open(&#39;vendors.txt&#39;, &quot;a+&quot; ) as myfile:
    myfile.write(&#39;Acer \n&#39;)
    myfile.write(&#39;Asus \n&#39;)</pre>



<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-46691" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-46691" aria-expanded="false">Show Output</button><div id="ac-46691" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>8
0
&#39;Razer Blade \nAmazon \n&#39;
6
6</code></pre>
</div></div>



<p></p>

<p></div></p>