Python-3.4.3/C3/Getting-started-with-files/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Hello Friends. Welcome to the tutorial on "Getting started with files".
00:07 At the end of this tutorial, you will learn to Open a file.
00:13 Read the contents of the file line by line.
00:16 Read the entire content of the file at once.
00:20 Append the lines of a file to a list and Close the file.
00:26 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3 and IPython 5.1.0

00:40 To practice this tutorial, you should know about

Lists and for statement

00:48 If not, see the pre-requisite Python tutorials on this website.
00:54 To open a file for reading or writing, we can use a built in function called open().
01:01 Open() function returns a file object.

The syntax is shown here.

Filename is the name of the file to be opened.

01:12 Mode- This indicates how the file is going to be opened.
01:17 r is for Read mode
01:20 w is for Write mode
01:23 a represents Appending mode and r+ for both Read and Write mode

Specifying mode is optional.

01:32 Let us open a file pendulum.txt in a text editor.
01:38 This file contains 2 data columns, length and time of pendulum.

We will be using this text file for our demonstration.

01:49 The file pendulum.txt is available in the Code File link of this tutorial.

Please download it in Home directory and use it.

02:00 Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
02:07 Type ipython3 and press Enter.
02:12 Let us initialise the pylab package.

Type %pylab and press Enter.

02:20 Let us clear the terminal.
02:23 Let us open the file pendulum.txt.
02:27 Type f is equal to open inside parentheses inside quotes pendulum dot txt
02:38 Here the mode is not specified. By default, it is ‘r’.
02:43 Let us type f on the terminal to see what it is.
02:48 The file object f shows the filename and mode of the file which is open.
02:57 'r' stands for read only mode.

As you can see, this file is open in read only mode.

03:06 Now let us learn to read the whole file into a single variable.
03:11 Type pend equal to f dot read open and close parentheses.
03:18 We use the read method to read all the contents of the file into the variable pend

Press Enter

03:27 To use read method we use the file object dot read method.
03:34 Now, let us see what pend contains, by typing print inside parentheses pend.

Press Enter

03:44 We can see that pend has all the data of the file pendulum.txt.
03:50 Type just pend to see more explicitly, what it contains.
03:56 We can see the newline characters as well, in the output.
04:01 Let us learn to split the variable pend into a list of lines in the file.
04:07 We use the method splitlines to split a file of data into list of lines.
04:14 For this we need to store this list in a variable, say pend_list
04:21 Type pend_list equal to pend dot splitlines open and close parentheses

Press Enter

04:33 Type pend underscore list

Press Enter

04:37 We got the data into list of lines.
04:41 pend_list does not contain newline characters like \n.
04:47 This is because the string pend is split on the newline characters.
04:53 Let us close the file opened into f.
04:57 Type f dot close open and close parentheses

Press Enter

05:04 It is a good programming practice to close any file objects that we have opened

after their job is done.

05:11 Pause the video here, try out the following exercise and then resume the video.
05:17 Re-open the file pendulum.txt with f as the file object.
05:23 Recall that we have closed the file earlier.
05:27 Let us switch back to the terminal.
05:30 Let us now move on to reading files line by line.
05:34 To re-open the file again, type

f is equal to open inside parentheses inside quotes pendulum.txt

05:47 Now, to read the file line-by-line, we iterate over the file object using the for loop.
05:54 Let us iterate over the file line-wise and print each of the lines.

Let us clear the terminal.

06:03 Type for line in f colon press Enter

four spaces print inside parentheses line

06:16 Here, line is a loop variable, and it is not a keyword.
06:21 We could have used any other variable name, but line seems meaningful enough.

Press Enter twice.

06:30 Instead of just printing the lines, let us append them into a list, say line_list.
06:37 We first initialize a line_list as an empty-list
06:42 Type, line underscore list is equal to open and close square brackets.

Press Enter

06:54 Type the code as

for line in open inside parentheses inside quotes pendulum dot txt colon press Enter

07:10 four space line underscore list dot append inside parentheses line

Press Enter twice.

07:23 Here, the for loop reads the file pendulum.txt line-by-line.
07:29 The append method will add each of the line to the list, line_list.
07:35 We could, as usual close the file using f.close() and re-open it.
07:42 But, this time, let's leave the file object f and directly open the file within the for statement.
07:50 This will save us the trouble of closing the file, each time we open it.
07:55 Let us see what line_list contains.

Type line underscore list and press Enter.

08:05 line underscore list is a list of the lines in the file, along with the newline characters.
08:13 We can strip out the newline characters from the lines by using some string methods.
08:20 This will be covered in the further tutorial on strings.
08:25 This brings us to the end of this tutorial.

In this tutorial, we learnt to - Open and close files using the open and close methods respectively.

08:38 Read the data in the files as a whole, by using the read method.
08:43 Read the data in the files line by line by iterating over the file object using the for loop.
08:50 Append the lines of a file to a list using the append method within the for loop.
08:56 Here are some self assessment questions for you to solve
09:01 1. The open function returns a

string, list, file object, function

09:07 2. What does the function splitlines() do.
09:11 Displays the data as strings all in a line
09:14 Displays the data line by line as strings
09:18 Displays the data line by line but not as strings
09:24 And the answers are,

1. open function returns a file object

09:31 splitlines() displays the data line by line as strings
09:37 Please post your timed queries in this forum.
09:41 Please post your general queries on Python in this forum.
09:46 FOSSEE team coordinates the TBC project.
09:50 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

10:01 Thats it for the tutorial. This is Trupti Kini from IIT Bombay (or FOSSEE, if you wish) signing off.

Thank you.

Contributors and Content Editors

PoojaMoolya