<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FGetting-started-with-files%2FEnglish</id>
		<title>Python/C3/Getting-started-with-files/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FGetting-started-with-files%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Getting-started-with-files/English&amp;action=history"/>
		<updated>2026-05-15T07:38:50Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Getting-started-with-files/English&amp;diff=503&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1  Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to the tutorial on &quot;G…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Getting-started-with-files/English&amp;diff=503&amp;oldid=prev"/>
				<updated>2012-11-29T06:19:15Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1  Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to the tutorial on &amp;quot;G…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1&lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello Friends and Welcome to the tutorial on &amp;quot;Getting started with files&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Open a file.&lt;br /&gt;
# Read the contents of the file line by line.&lt;br /&gt;
# Read the entire content of file at once.&lt;br /&gt;
# Append the lines of a file to a list.&lt;br /&gt;
# Close the file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite &lt;br /&gt;
&lt;br /&gt;
Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython -pylab&lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Getting started with Lists&amp;quot; and &amp;quot;Getting started with For&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Open the terminal and start ipython&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f = open('/home/fossee/pendulum.txt')&lt;br /&gt;
| Let us first open the file, &amp;lt;tt&amp;gt;pendulum.txt&amp;lt;/tt&amp;gt; present in &amp;lt;tt&amp;gt;/home/fossee/&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f&lt;br /&gt;
| Here &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; is called a file object. Let us type &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; on the terminal to see what it is.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| The file object shows the filepath and mode of the file which is open. 'r' stand for read only mode and 'w' stands for write mode. As you can see, this file is open in read only mode.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  pend = f.read()&lt;br /&gt;
| We shall first learn to read the whole file into a single variable. We use the &amp;lt;tt&amp;gt;read&amp;lt;/tt&amp;gt; method to read all the contents of the file into the variable, &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  print pend&lt;br /&gt;
| Now, let us see what &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt; contains, by typing &amp;lt;tt&amp;gt;print pend&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  pend&lt;br /&gt;
| We can see that &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt; has all the data of the file. Type just &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt; to see more explicitly, what it contains.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
Split the variable into a list, &amp;lt;tt&amp;gt;pend_list&amp;lt;/tt&amp;gt;, of the lines in the file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  pend_list = pend.splitlines()&lt;br /&gt;
 pend_list&lt;br /&gt;
| We use the function &amp;lt;tt&amp;gt;splitlines&amp;lt;/tt&amp;gt; to solve this problem.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f.close()&lt;br /&gt;
| Now, let us learn to read the file line-by-line. But, before that we will have to close the file, since the file has already been read till the end.&lt;br /&gt;
&lt;br /&gt;
Let us close the file opened into f.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f&lt;br /&gt;
| Again type &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; on the prompt to see what it contains.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Notice, that it now says the file has been closed. It is a good programming practice to close any file objects that we have opened, after their job is done.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| Let us, now move on to reading files line-by-line. Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
Re-open the file &amp;lt;tt&amp;gt;pendulum.txt&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; as the file object.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  f = open('/home/fossee/pendulum.txt')&lt;br /&gt;
| We just use the up arrow until we reach the open command and issue it again.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  for line in f:&lt;br /&gt;
     print line&lt;br /&gt;
| Now, to read the file line-by-line, we iterate over the file object line-by-line, using the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; command. Let us iterate over the file line-wise and print each of the lines.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| &amp;lt;tt&amp;gt;line&amp;lt;/tt&amp;gt; is a variable, sometimes called the loop variable, and it is not a keyword. We could have used any other variable name, but &amp;lt;tt&amp;gt;line&amp;lt;/tt&amp;gt; seems meaningful enough.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  &amp;lt;nowiki&amp;gt;line_list = [ ]&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
| Instead of just printing the lines, let us append them to a list, &amp;lt;tt&amp;gt;line_list&amp;lt;/tt&amp;gt;. We first initialize an empty list, &amp;lt;tt&amp;gt;line_list&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  for line in open('/home/fossee/pendulum.txt'):&lt;br /&gt;
     line_list.append(line)&lt;br /&gt;
| Let us then read the file line-by-line and then append each of the lines to the list. We could, as usual close the file using &amp;lt;tt&amp;gt;f.close&amp;lt;/tt&amp;gt; and re-open it. But, this time, let's leave alone the file object &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; and directly open the file within the for statement. This will save us the trouble of closing the file, each time we open it.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  line_list&lt;br /&gt;
| Let us see what &amp;lt;tt&amp;gt;line_list&amp;lt;/tt&amp;gt; contains.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Notice that &amp;lt;tt&amp;gt;line_list&amp;lt;/tt&amp;gt; is a list of the lines in the file, along with the newline characters. If you noticed, &amp;lt;tt&amp;gt;pend_list&amp;lt;/tt&amp;gt; did not contain the newline characters, because the string &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt;, was split on the newline characters.&lt;br /&gt;
&lt;br /&gt;
We can strip out the newline characters from the lines by using some string methods which we shall look in the further tutorial on strings.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we learnt to,&lt;br /&gt;
&lt;br /&gt;
# Open and close files using the &amp;lt;tt&amp;gt;open&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;close&amp;lt;/tt&amp;gt; functions respectively.&lt;br /&gt;
# Read the data in the files as a whole,by using the &amp;lt;tt&amp;gt;read&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
# Read the data in the files line by line by iterating over the file object using the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop.&lt;br /&gt;
# Append the lines of a file to a list using the &amp;lt;tt&amp;gt;append&amp;lt;/tt&amp;gt; function within the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# The &amp;lt;tt&amp;gt;open&amp;lt;/tt&amp;gt; function returns a&lt;br /&gt;
** string&lt;br /&gt;
** list&lt;br /&gt;
** file object&lt;br /&gt;
** function&lt;br /&gt;
# What does the function &amp;lt;tt&amp;gt;splitlines()&amp;lt;/tt&amp;gt; do.&lt;br /&gt;
** Displays the data as strings,all in a line&lt;br /&gt;
** Displays the data line by line as strings&lt;br /&gt;
** Displays the data line by line but not as strings&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# The function &amp;lt;tt&amp;gt;open&amp;lt;/tt&amp;gt;, returns a file object.&lt;br /&gt;
# The function &amp;lt;tt&amp;gt;splitlines&amp;lt;/tt&amp;gt; displays the data line by line as strings.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>