<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>file handling in python Archives -</title>
	<atom:link href="https://mitindia.in/tag/file-handling-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>https://mitindia.in/tag/file-handling-in-python/</link>
	<description></description>
	<lastBuildDate>Tue, 12 Nov 2019 06:25:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://mitindia.in/wp-content/uploads/2023/03/cropped-android-chrome-512x512-1-32x32.png</url>
	<title>file handling in python Archives -</title>
	<link>https://mitindia.in/tag/file-handling-in-python/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>File Handling in Python</title>
		<link>https://mitindia.in/file-handling-in-python/</link>
		
		<dc:creator><![CDATA[SKB]]></dc:creator>
		<pubDate>Tue, 12 Nov 2019 06:25:30 +0000</pubDate>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[file handling in python]]></category>
		<category><![CDATA[python file handling]]></category>
		<guid isPermaLink="false">http://www.mitindia.in/?p=1013</guid>

					<description><![CDATA[<p>Python File Handling What is File? The file is a collection of data used in almost all programming languages. Why File Handling? To handle large amounts of data to be stored in computer memory for future reference. What are  Modes of File? w = Write  / create file   a = append / add to [&#8230;]</p>
<p>The post <a href="https://mitindia.in/file-handling-in-python/">File Handling in Python</a> appeared first on <a href="https://mitindia.in"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><span style="color: #003366;">Python File Handling</span></h1>
<p><span style="color: #993300;">What is File?</span></p>
<p><span style="color: #008000;">The file is a collection of data used in almost all programming languages.</span></p>
<p><span style="color: #993300;">Why File Handling?</span></p>
<p><span style="color: #008000;">To handle large amounts of data to be stored in computer memory for future reference. </span></p>
<p><span style="color: #993300;">What are  Modes of File?</span></p>
<p><span style="color: #008000;">w = Write  / create file  </span></p>
<p><span style="color: #008000;">a = append / add to data to existing file</span></p>
<p><span style="color: #008000;">r = read / display information from file.</span></p>
<p><span style="color: #008000;">In python, both text and binary data can read and write.</span></p>
<p><span style="color: #993300;"><strong>The following are examples of File Handling in Python.</strong></span></p>
<div><strong><span style="color: #ff0000;"># File handling in python &#8211; Creating file and writing to file</span></strong></div>
<div>file=open(&#8216;D:\PythonExa\mytext.txt&#8217;, &#8216;w&#8217;)</div>
<div>file.write(&#8220;This is text is being inserted in text file&#8221;)</div>
<div>file.close()</div>
<div>print(&#8220;File created successfully!&#8221;)</div>
<div></div>
<blockquote>
<div><span style="color: #339966;"><strong>OUTPUT:</strong></span></div>
<div>File created successfully!</div>
<div></div>
</blockquote>
<div><strong><span style="color: #ff0000;">#Python File Handling &#8211; Storing numbers in File and Displaying using Loop. </span></strong></div>
<div>for x in range(11):</div>
<div>    with open(&#8216;nump.txt&#8217;, &#8216;a&#8217;) as f:</div>
<div>        f.write(&#8216;%d&#8217; %x)</div>
<div>        print(x)</div>
<div></div>
<div>print(&#8216;Done!&#8217;)</div>
<div></div>
<div>with open(&#8216;nump.txt&#8217;, &#8216;r+&#8217;) as f:</div>
<div>    print(f.read())</div>
<div></div>
<blockquote>
<div><strong><span style="color: #339966;">OUTPUT:</span></strong></div>
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>Done!</div>
<div>012345678910</div>
</blockquote>
<div></div>
<div></div>
<div><strong><span style="color: #ff0000;"># File handling in python &#8211; Appending file </span></strong></div>
<div>file=open(&#8216;D:\PythonExa\mytext.txt&#8217;, &#8216;a&#8217;)</div>
<div>file.write(&#8221; 12345678 Another line of text  is being inserted in text file&#8221;)</div>
<div>file.close()</div>
<div>print(&#8220;Data Added to File Successfully!&#8221;)</div>
<div></div>
<blockquote>
<div><strong><span style="color: #339966;">OUTPUT:</span></strong></div>
<div>Data Added to File Successfully!</div>
</blockquote>
<div></div>
<div></div>
<div><strong><span style="color: #ff0000;">#File handling in python &#8211; Reading data from a file </span></strong></div>
<div>with open(&#8220;D:\PythonExa\mytext.txt&#8221;) as file:</div>
<div>    data = file.read()</div>
<div># show data using print command</div>
<div>print(data)</div>
<div></div>
<blockquote>
<div><strong><span style="color: #339966;">OUTPUT:</span></strong></div>
<div>This is text is being inserted in text file 12345678 Another line of text  is being inserted in the text file</div>
<div></div>
</blockquote>
<div></div>
<div><strong><span style="color: #ff0000;"># Program to show various ways to read and  write data in a file. </span></strong></div>
<div>file1 = open(&#8220;mtext.txt&#8221;,&#8221;w&#8221;)</div>
<div>string = [&#8220;This is Web \n&#8221;,&#8221;This is Data \n&#8221;,&#8221;This is AI \n&#8221;]</div>
<div></div>
<div># \n is placed to indicate EOL (End of Line)</div>
<div>file1.write(&#8220;Hello \n&#8221;)</div>
<div>file1.writelines(string)</div>
<div>file1.close() #to change file access modes</div>
<div></div>
<div>file1 = open(&#8220;mtext.txt&#8221;,&#8221;r+&#8221;)</div>
<div></div>
<div>print (&#8220;Output of Read function is &#8220;)</div>
<div>print (file1.read())</div>
<div>print()</div>
<div></div>
<div># seek(n) takes the file handle to the nth bite from the beginning.</div>
<div>file1.seek(0)</div>
<div></div>
<div>print (&#8220;Output of Readline function is &#8220;)</div>
<div>print (file1.readline())</div>
<div>print()</div>
<div></div>
<div>file1.seek(0)</div>
<div># readlines function</div>
<div>print (&#8220;Output of Readlines function is &#8220;)</div>
<div>print (file1.readlines())</div>
<div>print()</div>
<div>file1.close()</div>
<div></div>
<blockquote>
<div><strong><span style="color: #339966;">OUTPUT:</span></strong></div>
<div>Output of Read function is</div>
<div>Hello</div>
<div>This is Web</div>
<div>This is Data</div>
<div>This is AI</div>
<div></div>
<div></div>
<div>Output of Readline function is</div>
<div>Hello</div>
<div></div>
<div>Output of Readlines function is</div>
<div>[&#8216;Hello \n&#8217;, &#8216;This is Web \n&#8217;, &#8216;This is Data \n&#8217;, &#8216;This is AI \n&#8217;]</div>
</blockquote>
<div></div>
<div><strong><span style="color: #ff0000;">#Store Name and Age using File handling in python &#8211; Appending file </span></strong></div>
<div>file=open(&#8216;D:\PythonExa\std_det.txt&#8217;, &#8216;a&#8217;)</div>
<div>nm=input(&#8216;Enter name:&#8217;)</div>
<div>ag=input(&#8216;Age:&#8217;)</div>
<div></div>
<div>file.write(nm)</div>
<div>file.write(&#8216;\t&#8217;)</div>
<div>file.write(ag)</div>
<div>file.close()</div>
<div></div>
<div>file=open(&#8216;D:\PythonExa\std_det.txt&#8217;, &#8216;r+&#8217;)</div>
<div>print(file.readline(), end=&#8217;\n&#8217;)</div>
<div>file.close()</div>
<div></div>
<div></div>
<blockquote>
<div><span style="color: #339966;">OUTPUT:</span></div>
<div>Enter name:Sharan</div>
<div>Age:21</div>
<div>shivaaa 21Ganesh 21Sharan 21</div>
</blockquote>
<div></div>
<div><strong><span style="color: #ff0000;">#Looping &#8211; printing series </span></strong></div>
<div>print(&#8220;Series &#8211; 1&#8221;)</div>
<div>for i in range(1, 5):</div>
<div>    for j in range(i):</div>
<div>         print(&#8216;*&#8217;, end=&#8217; &#8216;)</div>
<div>    print()</div>
<div></div>
<div>print(&#8220;Series &#8211; 2&#8221;)</div>
<div>for i in range(1, 5):</div>
<div>    for j in range(i):</div>
<div>         print(i, end=&#8217; &#8216;)</div>
<div>    print()</div>
<div></div>
<div>print(&#8220;Series &#8211; 3&#8221;)</div>
<div>for i in range(1, 5):</div>
<div>    for j in range(i):</div>
<div>         print(i+j, end=&#8217; &#8216;)</div>
<div>    print()</div>
<div></div>
<div></div>
<blockquote>
<div><span style="color: #339966;"><strong>OUTPUT:</strong></span></div>
<div>Series &#8211; 1</div>
<div>*</div>
<div>* *</div>
<div>* * *</div>
<div>* * * *</div>
<div>Series &#8211; 2</div>
<div>1</div>
<div>2 2</div>
<div>3 3 3</div>
<div>4 4 4 4</div>
<div>Series &#8211; 3</div>
<div>1</div>
<div>2 3</div>
<div>3 4 5</div>
<div>4 5 6 7</div>
</blockquote>
<p>&nbsp;</p>
<p><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fmitindia.in%2Ffile-handling-in-python%2F&amp;linkname=File%20Handling%20in%20Python" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fmitindia.in%2Ffile-handling-in-python%2F&amp;linkname=File%20Handling%20in%20Python" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fmitindia.in%2Ffile-handling-in-python%2F&#038;title=File%20Handling%20in%20Python" data-a2a-url="https://mitindia.in/file-handling-in-python/" data-a2a-title="File Handling in Python"><img src="https://static.addtoany.com/buttons/favicon.png" alt="Share"></a></p><p>The post <a href="https://mitindia.in/file-handling-in-python/">File Handling in Python</a> appeared first on <a href="https://mitindia.in"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
