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

From Script | Spoken-Tutorial
Jump to: navigation, search
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 "Getting started with files".
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. Open a file.
  2. Read the contents of the file line by line.
  3. Read the entire content of file at once.
  4. Append the lines of a file to a list.
  5. Close the file.


Show Slide 3

Pre-requisite

Switch to the terminal

ipython -pylab
Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists" and "Getting started with For".

Open the terminal and start ipython

f = open('/home/fossee/pendulum.txt') Let us first open the file, pendulum.txt present in /home/fossee/.
f Here f is called a file object. Let us type f on the terminal to see what it is.
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.
pend = f.read() We shall first learn to read the whole file into a single variable. We use the read method to read all the contents of the file into the variable, pend.
print pend Now, let us see what pend contains, by typing print pend
pend We can see that pend has all the data of the file. Type just pend to see more explicitly, what it contains.
Show Slide 4

Assignment 1

Pause the video here, try out the following exercise and resume the video.

Split the variable into a list, pend_list, of the lines in the file.

pend_list = pend.splitlines()
pend_list
We use the function splitlines to solve this problem.
f.close() 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.

Let us close the file opened into f.

f Again type f on the prompt to see what it contains.
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.
Show Slide 5

Assignment 2

Let us, now move on to reading files line-by-line. Pause the video here, try out the following exercise and resume the video.

Re-open the file pendulum.txt with f as the file object.

f = open('/home/fossee/pendulum.txt') We just use the up arrow until we reach the open command and issue it again.
for line in f:
    print line
Now, to read the file line-by-line, we iterate over the file object line-by-line, using the for command. Let us iterate over the file line-wise and print each of the lines.
line is a variable, sometimes called the loop variable, and it is not a keyword. We could have used any other variable name, but line seems meaningful enough.
line_list = [ ] Instead of just printing the lines, let us append them to a list, line_list. We first initialize an empty list, line_list.
for line in open('/home/fossee/pendulum.txt'):
    line_list.append(line)
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 f.close and re-open it. But, this time, let's leave alone the file object f and directly open the file within the for statement. This will save us the trouble of closing the file, each time we open it.
line_list Let us see what line_list contains.
Notice that line_list is a list of the lines in the file, along with the newline characters. If you noticed, pend_list did not contain the newline characters, because the string pend, was split on the newline characters.

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.

Show Slide 6

Summary slide

This brings us to the end of this tutorial. In this tutorial, we learnt to,
  1. Open and close files using the open and close functions respectively.
  2. Read the data in the files as a whole,by using the read function.
  3. Read the data in the files line by line by iterating over the file object using the for loop.
  4. Append the lines of a file to a list using the append function within the for loop.


Show Slide 7

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. The open function returns a
    • string
    • list
    • file object
    • function
  1. What does the function splitlines() do.
    • Displays the data as strings,all in a line
    • Displays the data line by line as strings
    • Displays the data line by line but not as strings


Show Slide 8

Solution of self assessment questions on slide

And the answers,
  1. The function open, returns a file object.
  2. The function splitlines displays the data line by line as strings.


Show Slide 9

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika