<?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-3.4.3%2FC3%2FGetting-started-with-files%2FEnglish-timed</id>
		<title>Python-3.4.3/C3/Getting-started-with-files/English-timed - 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-3.4.3%2FC3%2FGetting-started-with-files%2FEnglish-timed"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C3/Getting-started-with-files/English-timed&amp;action=history"/>
		<updated>2026-04-22T00:55:25Z</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-3.4.3/C3/Getting-started-with-files/English-timed&amp;diff=47635&amp;oldid=prev</id>
		<title>PoojaMoolya: Created page with &quot;{|- border=1 | &lt;center&gt;'''Time'''&lt;/center&gt; | &lt;center&gt;'''Narration'''&lt;/center&gt;  |- | 00:01 | Hello Friends. Welcome to the tutorial on &quot;Getting started with '''files'''&quot;.   |-...&quot;</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C3/Getting-started-with-files/English-timed&amp;diff=47635&amp;oldid=prev"/>
				<updated>2019-05-30T12:20:46Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{|- border=1 | &amp;lt;center&amp;gt;&amp;#039;&amp;#039;&amp;#039;Time&amp;#039;&amp;#039;&amp;#039;&amp;lt;/center&amp;gt; | &amp;lt;center&amp;gt;&amp;#039;&amp;#039;&amp;#039;Narration&amp;#039;&amp;#039;&amp;#039;&amp;lt;/center&amp;gt;  |- | 00:01 | Hello Friends. Welcome to the tutorial on &amp;quot;Getting started with &amp;#039;&amp;#039;&amp;#039;files&amp;#039;&amp;#039;&amp;#039;&amp;quot;.   |-...&amp;quot;&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;
| &amp;lt;center&amp;gt;'''Time'''&amp;lt;/center&amp;gt;&lt;br /&gt;
| &amp;lt;center&amp;gt;'''Narration'''&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:01&lt;br /&gt;
| Hello Friends. Welcome to the tutorial on &amp;quot;Getting started with '''files'''&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:07&lt;br /&gt;
| At the end of this tutorial, you will learn to Open a file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:13&lt;br /&gt;
| Read the contents of the file line by line.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:16&lt;br /&gt;
| Read the entire content of the file at once.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:20&lt;br /&gt;
| Append the lines of a file to a '''list''' and Close the file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:26&lt;br /&gt;
| To record this tutorial, I am using &lt;br /&gt;
&lt;br /&gt;
'''Ubuntu Linux 16.04''' operating system&lt;br /&gt;
&lt;br /&gt;
'''Python 3.4.3 '''and '''IPython 5.1.0''' &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:40&lt;br /&gt;
| To practice this tutorial, you should know about&lt;br /&gt;
&lt;br /&gt;
'''Lists''' and '''for statement'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:48&lt;br /&gt;
|If not, see the pre-requisite '''Python''' tutorials on this website.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 00:54&lt;br /&gt;
| To open a file for reading or writing, we can use a '''built in function''' called '''open()'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:01&lt;br /&gt;
|'''Open() function''' returns a '''file object'''.&lt;br /&gt;
&lt;br /&gt;
The syntax is shown here.&lt;br /&gt;
&lt;br /&gt;
'''Filename'''  is the name of the file to be opened.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:12&lt;br /&gt;
|'''Mode'''- This indicates how the file is going to be opened.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:17&lt;br /&gt;
| r is for '''Read mode'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:20&lt;br /&gt;
|w is for '''Write mode''' &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:23&lt;br /&gt;
|a represents '''Appending mode''' and r+  for both '''Read and Write mode'''&lt;br /&gt;
&lt;br /&gt;
Specifying '''mode''' is optional.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:32&lt;br /&gt;
| Let us open a file '''pendulum.txt''' in a text editor.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:38&lt;br /&gt;
| This file contains 2 data columns, '''length''' and '''time''' of '''pendulum'''.&lt;br /&gt;
&lt;br /&gt;
We will be using this text file for our demonstration.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 01:49&lt;br /&gt;
| The file '''pendulum.txt''' is available in the '''Code File link''' of this tutorial. &lt;br /&gt;
&lt;br /&gt;
Please download it in '''Home directory''' and use it.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:00&lt;br /&gt;
|  Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:07&lt;br /&gt;
|Type '''ipython3''' and press '''Enter'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:12&lt;br /&gt;
|  Let us initialise the '''pylab package'''.&lt;br /&gt;
&lt;br /&gt;
Type '''%pylab '''and press''' Enter.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:20&lt;br /&gt;
|Let us clear the '''terminal'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:23&lt;br /&gt;
|  Let us open the file '''pendulum.txt.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:27&lt;br /&gt;
|  Type '''f''''' is equal to''''' open''''' inside parentheses inside quotes '''''pendulum '''''dot '''''txt'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:38&lt;br /&gt;
|Here the '''mode''' is not specified. By default, it is '''‘r’'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:43&lt;br /&gt;
| Let us type '''f''' on the '''terminal''' to see what it is.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:48&lt;br /&gt;
|The '''file object f''' shows the filename and '''mode''' of the file which is open. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 02:57&lt;br /&gt;
| ''''r' '''stands for '''read only mode'''.&lt;br /&gt;
&lt;br /&gt;
As you can see, this '''file''' is open in '''read only mode'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:06&lt;br /&gt;
| Now let us learn to read the whole '''file''' into a single '''variable'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:11&lt;br /&gt;
| Type '''pend '''''equal to '''''f '''''dot '''''read'' '''open and close parentheses.''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:18&lt;br /&gt;
|We use the '''read method''' to read all the contents of the file into the '''variable pend'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:27&lt;br /&gt;
|To use '''read method''' we use the '''file object dot read method'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:34&lt;br /&gt;
| Now, let us see what '''pend '''contains, by typing '''print '''''inside parentheses '''''pend.'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:44&lt;br /&gt;
| We can see that '''pend''' has all the data of the '''file pendulum.txt'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:50&lt;br /&gt;
| Type just '''pend''' to see more explicitly, what it contains.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 03:56&lt;br /&gt;
|We can see the '''newline characters''' as well, in the output.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:01&lt;br /&gt;
| Let us learn to split the '''variable pend''' into a '''list''' of lines in the file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:07&lt;br /&gt;
|We use the '''method splitlines''' to split a file of data into '''list''' of lines.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:14&lt;br /&gt;
| For this we need to store this '''list '''in a '''variable''', say '''pend_list'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:21&lt;br /&gt;
|Type '''pend_list '''''equal to''''' pend '''''dot '''''splitlines '''''open and close parentheses''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:33&lt;br /&gt;
| Type '''pend '''''underscore '''''list '''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:37&lt;br /&gt;
| We got the data into '''list''' of lines.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:41&lt;br /&gt;
| '''pend_list''' does not contain '''newline characters '''like '''\n'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:47&lt;br /&gt;
|This is because the '''string pend''' is split on the '''newline characters'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:53&lt;br /&gt;
| Let us close the '''file''' opened into '''f'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 04:57&lt;br /&gt;
|Type '''f  dot close ''' ''open and close parentheses''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:04&lt;br /&gt;
| It is a good programming practice  to '''close''' any '''file objects''' that we have opened&lt;br /&gt;
&lt;br /&gt;
after their job is done.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:11&lt;br /&gt;
| Pause the video here, try out the following exercise and then resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:17&lt;br /&gt;
|Re-open the file '''pendulum.txt''' with '''f''' as the '''file object'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:23&lt;br /&gt;
| Recall that we have closed the file earlier.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:27&lt;br /&gt;
|Let us switch back to the terminal.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:30&lt;br /&gt;
| Let us now move on to reading files line by line. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:34&lt;br /&gt;
| To re-open the file again, type &lt;br /&gt;
&lt;br /&gt;
'''f '''''is equal to '''''open '''''inside parentheses inside quotes '''''pendulum.txt'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:47&lt;br /&gt;
| Now, to read the file line-by-line, we '''iterate''' over the '''file object''' using the '''for loop'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 05:54&lt;br /&gt;
|Let us '''iterate''' over the '''file''' line-wise and '''print''' each of the lines.&lt;br /&gt;
&lt;br /&gt;
Let us clear the '''terminal'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:03&lt;br /&gt;
|Type '''for line in f colon''' press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
'''four spaces print inside parentheses line'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:16&lt;br /&gt;
| Here, '''line''' is a '''loop variable''', and it is not a '''keyword'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:21&lt;br /&gt;
|We could have used any other '''variable''' name, but '''line''' seems meaningful enough.&lt;br /&gt;
&lt;br /&gt;
Press '''Enter''' twice.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:30&lt;br /&gt;
| Instead of just printing the lines, let us append them into a '''list''', say '''line_list'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:37&lt;br /&gt;
| We first '''initialize''' a '''line_list '''as an '''empty-list'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 06:42&lt;br /&gt;
|Type, '''line ''underscore ''list ''is'' ''equal to ''open and close square brackets.''' &lt;br /&gt;
Press Enter&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:54&lt;br /&gt;
| Type the code as&lt;br /&gt;
&lt;br /&gt;
'''for line in open ''inside parentheses inside quotes ''pendulum ''dot'' txt colon''' press '''Enter'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:10&lt;br /&gt;
|'''''four space'' line'' underscore'' list ''dot ''append ''inside parentheses'' line'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter''' twice.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:23&lt;br /&gt;
|Here, the '''for loop '''reads the file '''pendulum.txt '''line-by-line.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:29&lt;br /&gt;
|The '''append method''' will add each of the line to the '''list, line_list.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:35&lt;br /&gt;
| We could, as usual close the file using '''f.close() '''and re-open it. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:42&lt;br /&gt;
| But, this time, let's leave the '''file object f '''and directly open the file within the '''for statement'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:50&lt;br /&gt;
|This will save us the trouble of closing the file, each time we open it.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 07:55&lt;br /&gt;
| Let us see what '''line_list''' contains.&lt;br /&gt;
&lt;br /&gt;
Type '''line ''underscore '' list '''and press '''Enter.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:05&lt;br /&gt;
| '''line ''underscore '' list''' is a '''list''' of the lines in the file, along with the '''newline characters'''. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:13&lt;br /&gt;
| We can strip out the '''newline characters''' from the lines by using some '''string methods'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:20&lt;br /&gt;
|This will be covered in the further tutorial on '''strings'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:25&lt;br /&gt;
| This brings us to the end of this tutorial. &lt;br /&gt;
&lt;br /&gt;
In this tutorial, we learnt to - Open and close files using the '''open''' and '''close methods''' respectively.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:38&lt;br /&gt;
|Read the data in the files as a whole, by using the '''read method'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:43&lt;br /&gt;
| Read the data in the files line by line by iterating over the '''file object''' using the '''for loop'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:50&lt;br /&gt;
| Append the lines of a file to a '''list''' using the '''append method''' within the '''for loop'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 08:56&lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:01&lt;br /&gt;
| 1. The '''open function''' returns a&lt;br /&gt;
&lt;br /&gt;
'''string''', '''list''', '''file object''', '''function'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:07&lt;br /&gt;
| 2. What does the '''function splitlines()''' do.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:11&lt;br /&gt;
| Displays the data as '''strings''' all in a line&lt;br /&gt;
 &lt;br /&gt;
|-&lt;br /&gt;
| 09:14&lt;br /&gt;
|Displays the data line by line as '''strings'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:18&lt;br /&gt;
| Displays the data line by line but not as '''strings'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:24&lt;br /&gt;
| And the answers are,&lt;br /&gt;
 &lt;br /&gt;
1. '''open function''' returns a '''file object'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:31&lt;br /&gt;
| '''splitlines() '''displays the data line by line as '''strings'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:37&lt;br /&gt;
| Please post your timed queries in this forum.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:41&lt;br /&gt;
| Please post your general queries on Python in this forum.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:46&lt;br /&gt;
| FOSSEE team coordinates the TBC project.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 09:50&lt;br /&gt;
| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.&lt;br /&gt;
&lt;br /&gt;
For more details, visit this website.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 10:01&lt;br /&gt;
| Thats it for the tutorial. This is Trupti Kini from IIT Bombay (or FOSSEE, if you wish) signing off.&lt;br /&gt;
&lt;br /&gt;
Thank you.&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>PoojaMoolya</name></author>	</entry>

	</feed>