Difference between revisions of "Python-3.4.3/C2/Getting-started-with-files/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Python/C2/Getting-started-with-files/English''''''Title of script''': Getting started with '''files''' '''Author: Trupti Kini''' '''Keywords: Python, IPython, files, '''...")
 
Line 61: Line 61:
 
Highlight according to narration: (open and fileobject)
 
Highlight according to narration: (open and fileobject)
  
'''''sy'''''doneIf you explain the syntax, then the actual code, it is easy to follow. Get suggestion from the content creator'''''ntax:'''''
+
'''''syntax:'''''
  
  
Line 94: Line 94:
 
Open a file
 
Open a file
  
'''''sy'''''doneIf you explain the syntax, then the actual code, it is easy to follow. Get suggestion from the content creator'''''ntax:'''''
+
'''''syntax:'''''
  
  
Line 105: Line 105:
 
a -Appending mode
 
a -Appending mode
  
r+ - Read and Writewe are specifying the default mode below. So it is not required here.
+
r+ - Read and Write
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| r- represent the Read mode
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| r- represent the Read mode
  
Line 116: Line 116:
  
 
Specifying mode is optional.
 
Specifying mode is optional.
 
Default mode is r.
 
  
 
|-
 
|-
Line 178: Line 176:
  
  
'''If so, specify that the default mode is r, read mode''''''f = open('pendulum.txt') - should we specify the mode here or leave it empty. if you don't sepcify, is it 'r' mode by default.''''''yes coz that will come when we just type f and press enter''''''Do we need to specify the mode like r, w, a, r+ in the open command'''f = open('pendulum.txt')'''
+
'''f = open('pendulum.txt')'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Type  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Type  
  
Line 272: Line 270:
  
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| doneIt would be better if you teach the method splitlines and then give an example. After that give some exercise for the user to solve. Please check the entire script for the same.<nowiki>[IPython Terminal]</nowiki>doneIt would be better if you teach the method splitlines and then give an example. After that give some exercise for the user to solve. Please check the entire script for the same.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki>
  
  
Line 279: Line 277:
  
  
We use the method '''splitlines''' to '''split''' a '''file''' of data into '''list''' of lines.doneIt would be better if you teach the method splitlines and then give an example. After that give some exercise for the user to solve. Please check the entire script for the same.
+
We use the method '''splitlines''' to '''split''' a '''file''' of data into '''list''' of lines.
  
 
|-
 
|-
Line 293: Line 291:
  
 
Highlight the output
 
Highlight the output
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| doneIt would be better if you teach the method splitlines and then give an example. After that give some exercise for the user to solve. Please check the entire script for the same.For this we need to store this '''list '''in a variable, say '''pend_list'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| For this we need to store this '''list '''in a variable, say '''pend_list'''
  
  
Line 406: Line 404:
 
Highlight '''line'''
 
Highlight '''line'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Instead of just printing the lines, let us append them to a list, say '''line_list'''.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Instead of just printing the lines, let us append them to a list, say '''line_list'''.  
 
 
 
  
 
|-
 
|-
Line 447: Line 442:
  
 
The '''append''' method will add each of the line to the list, '''line_list.'''
 
The '''append''' method will add each of the line to the list, '''line_list.'''
 
 
Let us then '''read''' the '''file''' line-by-line and then '''append''' each of the lines to the '''line_list'''.
 
 
 
for line in open ''inside parentheses inside quotes ''pendulum ''dot'' txt colon ''press enter''
 
 
''four space'' line'' underscore'' list ''dot ''append i''nside parentheses'' line
 
  
 
|-
 
|-

Revision as of 19:58, 8 January 2018

'Python/C2/Getting-started-with-files/English'Title of script: Getting started with files

Author: Trupti Kini

Keywords: Python, IPython, files,

Visual Cue
Narration
Show Slide Hello Friends. Welcome to the tutorial on "Getting started with files".
Show Slide

Objectives


At the end of this tutorial, you will be able learn to -


  1. Open a file.
  2. Read the contents of the file line by line.
  3. Read the entire content of the file at once.
  4. Append the lines of a file to a list.
  5. Close the file.


Show slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3
  • IPython 5.1.0


Show Slide:

Pre-requisite

To practice this tutorial, you should know about how to-
  • Lists and
  • for statement

If not, see the pre-requisite Python tutorials on http://spoken-tutorial.org

Show slide

Open a file


Highlight according to narration: (open and fileobject)

syntax:


file_object = open(“filename”, “mode”)


Filename - name of the file to be opened.

Mode- indicates how the file is going to be opened.


To open a file for reading or writing, we can use a built in function called open().


Open() function returns a file object.


The syntax is shown here:


Filename - is the name of the file to be opened.

Mode- This indicates how the file is going to be opened.



Show slide

Open a file

syntax:


f = open(“filename”, “mode”)

r- Read mode

w- Write mode

a -Appending mode

r+ - Read and Write

r- represent the Read mode

w- is for Write mode

a - represents Appending mode and

r+ - for both Read and Write mode


Specifying mode is optional.

Open pendulum.txt in text editor Let us open a file, pendulum.txt in a text editor.



Point to the columns in the file This file contains 2 data columns, length and time of pendulum.


We will be using this text file for our demonstration.



The file pendulum.txt is available in the code file link of this tutorial.


Please download it in Home directory and use it.



[Terminal]

ipython3


Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.


Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.


Type %pylab and press Enter.

Let us open the file, pendulum.txt.



[IPython Terminal]


f = open('pendulum.txt')

Type


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


Here the mode is not specified. By default, it is ‘r’.

[IPython Terminal]

f


Let us type f on the terminal to see what it is.


The file object f, shows the filepath and mode of the file, which is open.

[IPython Terminal]

Highlight filepath and mode 'r'

'r' stands for read only mode.


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

[IPython Terminal]


Now let us learn to read the whole file into a single variable.



type

pend = f.read()


Highlight pend= f.read()


Type


pend equal to f dot read open and close parentheses.


We use the read method to read all the contents of the file into the variable, pend


Press Enter


To use read method we use the file object dot read method.

[IPython Terminal]


print(pend)

Now, let us see what pend contains, by typing

print inside parentheses pend.


Press Enter

Point to the output We can see that pend has all the data of the file pendulum .txt.



[IPython Terminal]


pendHighlight output

Type just pend to see more explicitly, what it contains.


We can see the newline characters as well in the output.

[IPython Terminal]


Let us learn to split the variable pend into a list of lines in the file.


We use the method splitlines to split a file of data into list of lines.

[IPython Terminal]


Type

pend_list = pend.splitlines()

pend_list


Highlight the output

For this we need to store this list in a variable, say pend_list


Type

pend_list equal to pend dot splitlines open and close parentheses

Press Enter


Type

pend underscore list

Press Enter


We got the data into list of lines.

Highlight output pend_list does not contain newline characters like \n.


This is because the string pend is split on the newline characters.

Now, let us learn to read the file line-by-line.
[IPython Terminal]


f.close()

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.

Type f dot close open and close parentheses

[IPython Terminal]


Highlight closed file

It is a good programming practice to close any file objects that we have opened, after their job is done.
Let us, now move on to reading files line-by-line.
Show Slide

Exercise 1


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.

Recall, that we have closed the file earlier.

[IPython Terminal]

f = open('pendulum.txt')

To re-open the file again, type

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

[IPython Terminal]


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 loop.


Let us iterate over the file line-wise and print each of the lines.


Type

for line in f colon press Enter

four spaces print inside parentheses line

[IPython Terminal]


Highlight line

Here, line is a loop variable, and it is not a keyword.


We could have used any other variable name, but line seems meaningful enough.

[IPython Terminal]


Highlight line

Instead of just printing the lines, let us append them to a list, say line_list.
[IPython Terminal]


line_list = []

We first initialize an line_list as an empty-list


Type,

line underscore list equal to open and close square brackets.

[IPython Terminal]


Highlight according to narration.


for line in open('pendulum.txt'):

line_list.append(line)


Type the code as


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

four space line underscore list dot append inside parentheses line


Here, the for loop reads the file pendulum.txt line-by-line .


The append method will add each of the line to the list, line_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.

[IPython Terminal]


Type line_list and press Enter

Highlight output

Let us see what line_list contains.


Type line_list and press Enter.

[IPython Terminal]


Highlight newline character

If you noticed, line_list is a list of the lines in the file, along with the newline characters.
We can strip out the newline characters from the lines by using some string methods.

This will be covered in the further tutorial on strings.

Show Slide

Summary


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 methods respectively.
  2. Read the data in the files as a whole, by using the read method.


Show Slide

Summary

# Read the data in the files line by line by iterating over the file object using the for loop.
  1. Append the lines of a file to a list using the append method within the for loop.


Show Slide

Assignment


Here are some self assessment questions for you to solve
  1. The open function returns a
  • string
  • list
  • file object
  • function

2. 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


Solutions

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


Show Slide

Forum

Please post your timed queries in this forum.
Show Slide

Fossee Forum

Please post your general queries on Python in this forum.
Show Slide

Textbook Companion

FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.

Thank you.

Contributors and Content Editors

Nancyvarkey, Trupti