Difference between revisions of "Python-3.4.3/C2/Getting-started-with-files/English"
(Created page with "'''Python/C2/Getting-started-with-files/English''''''Title of script''': Getting started with '''files''' '''Author: Trupti Kini''' '''Keywords: Python, IPython, files, '''...") |
|||
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | '''Title of script''': '''Getting started with files''' | |
'''Author: Trupti Kini''' | '''Author: Trupti Kini''' | ||
− | '''Keywords: | + | '''Keywords: IPython, files, read, write, append, mode, splitlines()''' |
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center> | ||
Line 19: | Line 19: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| At the end of this tutorial, you will | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| At the end of this tutorial, you will learn to - |
− | + | ||
− | + | ||
# Open a file. | # Open a file. | ||
# Read the contents of the file line by line. | # Read the contents of the file line by line. | ||
# Read the entire content of the file at once. | # Read the entire content of the file at once. | ||
− | # Append the lines of a file to a list | + | # Append the lines of a file to a '''list''' and |
# Close the file. | # Close the file. | ||
− | |||
− | |||
|- | |- | ||
Line 37: | Line 33: | ||
* '''Ubuntu Linux 16.04''' operating system | * '''Ubuntu Linux 16.04''' operating system | ||
− | * '''Python 3.4.3''' | + | * '''Python 3.4.3 '''and |
− | * '''IPython 5.1.0''' | + | * '''IPython 5.1.0'''<br/> |
− | + | ||
− | + | ||
|- | |- | ||
Line 46: | Line 40: | ||
Pre-requisite | Pre-requisite | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practice this tutorial, you should know about | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practice this tutorial, you should know about |
− | * Lists and | + | * '''Lists''' and |
− | * '''for''' | + | * '''for statement''' |
− | If not, see the pre-requisite Python tutorials on | + | If not, see the pre-requisite '''Python''' tutorials on this website. |
|- | |- | ||
Line 59: | Line 53: | ||
− | Highlight according to narration: (open and | + | Highlight according to narration: (open and file object) |
− | ''''' | + | '''''syntax:''''' |
− | '''''file_object <nowiki>= open( | + | '''''file_object <nowiki>= open(</nowiki>filename, mode)''''' |
Filename - name of the file to be opened. | Filename - name of the file to be opened. | ||
− | Mode- indicates how the file is going to be opened. | + | Mode - indicates how the file is going to be opened. |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To open a file for reading or writing, we can use a '''built in function''' called '''open()'''. | |
− | + | ||
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To open a file for reading or writing, we can use a built in function called open(). | + | |
− | Open() function returns a file object. | + | '''Open() function''' returns a '''file object'''. |
− | The syntax is shown here | + | 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. | ||
|- | |- | ||
Line 94: | Line 85: | ||
Open a file | Open a file | ||
− | ''''' | + | '''''syntax:''''' |
− | ''''' | + | '''''file_object <nowiki>= open(filename, mode)</nowiki>''''' |
r- Read mode | r- Read mode | ||
Line 105: | Line 96: | ||
a -Appending mode | a -Appending mode | ||
− | r+ - Read and | + | 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- | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| r- is for '''Read mode''' |
− | w- is for Write mode | + | w- is for '''Write mode''' |
− | a - represents Appending mode and | + | a - represents '''Appending mode''' and |
− | r+ - for both Read and Write mode | + | r+ - for both '''Read and Write mode''' |
− | Specifying mode is optional | + | Specifying '''mode''' is optional. |
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open pendulum.txt in text editor | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open pendulum.txt in text editor | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us open a file | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us open a file '''pendulum.txt''' in a text editor. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Point to the columns in the file | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Point to the columns in the file | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This file contains 2 data columns, length and time of pendulum. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This file contains 2 data columns, '''length''' and '''time''' of '''pendulum'''. |
We will be using this text file for our demonstration. | We will be using this text file for our demonstration. | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Text box on the screen as per the narration |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The file '''pendulum.txt''' is available in the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The file '''pendulum.txt''' is available in the '''Code File link''' of this tutorial. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | Please download it in '''Home directory''' and use it. | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Terminal]</nowiki> | ||
− | ''' | + | Press '''Ctrl+Alt+T '''keys |
− | + | ||
− | + | ||
+ | Type '''ipython3''' | ||
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. | ||
− | + | Type '''ipython3''' and press '''Enter'''. | |
|- | |- | ||
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| <nowiki>[IPython console]</nowiki> | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| <nowiki>[IPython console]</nowiki> | ||
+ | |||
+ | Type | ||
'''%pylab '''and press '''Enter.''' | '''%pylab '''and press '''Enter.''' | ||
− | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us initialise the '''pylab''' | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us initialise the '''pylab package'''. |
Type '''%pylab '''and press''' Enter.''' | Type '''%pylab '''and press''' Enter.''' | ||
+ | |||
+ | Let us clear the '''terminal'''. | ||
|- | |- | ||
− | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| 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;"| Let us open the file | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us open the file '''pendulum.txt.''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 178: | Line 158: | ||
− | + | Type '''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 185: | Line 165: | ||
− | Here the | + | Here the '''mode''' is not specified. |
+ | |||
+ | By default, it is '''‘r’'''. | ||
|- | |- | ||
| 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> | | 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> | ||
− | '''f''' | + | Type '''f''' |
− | + | ||
− | + | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us type '''f''' on the '''terminal''' to see what it is. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us type '''f''' on the '''terminal''' to see what it is. | ||
− | The file object f | + | The '''file object f''' shows the filename and '''mode''' of the file which is open. |
|- | |- | ||
| 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> | | 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> | ||
− | Highlight ''' | + | Highlight '''filename '''and '''mode 'r'''' |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| ''''r' '''stands for '''read | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| ''''r' '''stands for '''read only mode'''. |
− | + | As you can see, this '''file''' is open in '''read only mode'''. | |
− | As you can see, this '''file''' is | + | |
|- | |- | ||
Line 213: | Line 192: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now let us learn to | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now let us learn to read the whole '''file''' into a single '''variable'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type |
'''pend = f.read()''' | '''pend = f.read()''' | ||
Line 228: | Line 204: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type |
− | + | ||
− | + | ||
'''pend '''''equal to '''''f '''''dot '''''read'' '''open and close parentheses.'' | '''pend '''''equal to '''''f '''''dot '''''read'' '''open and close parentheses.'' | ||
− | We use the '''read method''' to | + | We use the '''read method''' to read all the contents of the file into the '''variable pend''' |
Line 240: | Line 214: | ||
− | To use read method we use the file object dot read method. | + | To use '''read method''' we use the '''file object dot read method'''. |
|- | |- | ||
| 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> | | 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> | ||
+ | Type | ||
'''print(pend)''' | '''print(pend)''' | ||
Line 255: | Line 230: | ||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Point to the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Point to the data |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We can see that '''pend''' has all the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We can see that '''pend''' has all the data of the '''file pendulum.txt'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 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> | | 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> | ||
+ | Type | ||
− | '''pend'''Highlight | + | '''pend'''Highlight data with newline characters |
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type just '''pend''' to see more explicitly, what it contains. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type just '''pend''' to see more explicitly, what it contains. | ||
− | We can see the newline characters as well in the output. | + | We can see the '''newline characters''' as well, in 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;"| | + | | 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> |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us learn to split the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us learn to split the '''variable pend''' into a '''list''' of lines in the file. |
− | We use the | + | We use the '''method splitlines''' to split a file of data into '''list''' of lines. |
|- | |- | ||
Line 288: | Line 261: | ||
'''pend_list = pend.splitlines()''' | '''pend_list = pend.splitlines()''' | ||
− | + | | 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''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | |
− | + | ||
Type | Type | ||
Line 302: | Line 269: | ||
Press '''Enter''' | Press '''Enter''' | ||
− | + | |- | |
− | Type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type '''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;"| | |
− | '''pend '''''underscore '''''list ''' | + | Type '''pend '''''underscore '''''list ''' |
Press '''Enter''' | Press '''Enter''' | ||
− | + | |- | |
− | We got the data into list of lines. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"|Highlight the data into list of lines |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We got the data into '''list''' of lines. | ||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight the data into list of lines |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''pend_list''' does not contain '''newline | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''pend_list''' does not contain '''newline characters '''like '''\n'''. |
− | This is because the | + | This is because the '''string pend''' is split on the '''newline characters'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| |
+ | But, before that we will have to close the file, since the file has already been read till the end. | ||
|- | |- | ||
Line 328: | Line 297: | ||
'''f.close()''' | '''f.close()''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us close the '''file''' opened into '''f'''. |
+ | Type '''f dot close ''' ''open and close parentheses'' | ||
− | + | Press '''Enter''' | |
− | + | ||
− | + | ||
|- | |- | ||
Line 340: | Line 308: | ||
Highlight '''closed file''' | Highlight '''closed file''' | ||
− | |||
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| It is a good programming practice | |
+ | * to '''close''' any '''file objects''' that we have opened | ||
+ | * after their job is done. | ||
|- | |- | ||
Line 353: | Line 322: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Pause the video here, try out the following exercise and resume the video. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Pause the video here, try out the following exercise and then resume the video. |
− | Re-open the file '''pendulum.txt''' with '''f''' as the file object. | + | Re-open the file '''pendulum.txt''' with '''f''' as the '''file object'''. |
− | Recall | + | Recall that we have closed the file earlier. |
+ | |||
+ | Let us switch back to the terminal. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us now move on to reading files line- by - line. | ||
|- | |- | ||
| 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> | | 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> | ||
− | '''f = open('pendulum.txt')''' | + | Type'''f = open('pendulum.txt')''' |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To re-open the file again, type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| |
+ | |||
+ | To re-open the file again, type | ||
'''f '''''is equal to '''''open '''''inside parentheses inside quotes '''''pendulum.txt''' | '''f '''''is equal to '''''open '''''inside parentheses inside quotes '''''pendulum.txt''' | ||
Line 371: | Line 348: | ||
| 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> | | 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> | ||
+ | Type | ||
'''for line in f:''' | '''for line in f:''' | ||
Line 376: | Line 354: | ||
'''print(line)''' | '''print(line)''' | ||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now, to read the file line-by-line, we '''iterate''' over the '''file object''' using the '''for loop'''. | |
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now, to | + | |
Let us '''iterate''' over the '''file''' line-wise and '''print''' each of the lines. | Let us '''iterate''' over the '''file''' line-wise and '''print''' each of the lines. | ||
+ | |||
+ | Let us clear the '''terminal'''. | ||
Type | Type | ||
− | '''for line in f ' | + | '''for line in f colon''' press '''Enter''' |
− | ''four spaces | + | '''four spaces print inside parentheses line''' |
|- | |- | ||
Line 395: | Line 373: | ||
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;"| Here, '''line''' is a '''loop | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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. | We could have used any other '''variable''' name, but '''line''' seems meaningful enough. | ||
+ | |||
+ | Press '''Enter''' twice. | ||
|- | |- | ||
Line 405: | Line 385: | ||
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 | + | | 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 into 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;"| <nowiki>[IPython Terminal]</nowiki> | | 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> | ||
+ | Type | ||
'''<nowiki>line_list = []</nowiki>''' | '''<nowiki>line_list = []</nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We first '''initialize''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We first '''initialize''' a '''line_list '''as an '''empty-list''' |
Type, | Type, | ||
− | line ''underscore ''list ''equal to ''open and close square brackets. | + | '''line ''underscore ''list ''is'' ''equal to ''open and close square brackets.''' |
+ | Press Enter | ||
|- | |- | ||
Line 428: | Line 407: | ||
Highlight according to narration. | Highlight according to narration. | ||
+ | Type | ||
'''for line in open('pendulum.txt'):''' | '''for line in open('pendulum.txt'):''' | ||
Line 438: | Line 418: | ||
− | for line in open ''inside parentheses inside quotes ''pendulum ''dot'' txt colon ''press | + | '''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''' | |
+ | Press '''Enter''' twice. | ||
− | |||
+ | 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.''' |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We could, as usual | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We could, as usual close the file using '''f.close() '''and re-open it. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| But, this time, let's leave | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| But, this time, let's leave the '''file object f '''and directly open the file within the '''for statement'''. |
Line 473: | Line 447: | ||
Type '''line_list '''and press '''Enter''' | Type '''line_list '''and press '''Enter''' | ||
− | Highlight | + | Highlight data |
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us see what '''line_list''' contains. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us see what '''line_list''' contains. | ||
− | Type ''' | + | Type '''line ''underscore '' list '''and press '''Enter.''' |
|- | |- | ||
Line 483: | Line 457: | ||
− | Highlight '''newline | + | Highlight '''newline characters''' |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''line ''underscore '' list''' is a '''list''' of the lines in the file, along with the '''newline characters'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We can | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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. | + | This will be covered in the further tutorial on '''strings'''. |
|- | |- | ||
Line 499: | Line 473: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This brings us to the end of this tutorial | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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. | ||
+ | * Read the data in the files as a whole, by using the '''read method'''. | ||
|- | |- | ||
Line 510: | Line 484: | ||
Summary | Summary | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| |
− | + | ||
− | + | ||
+ | * Read the data in the files line by line by iterating over the '''file object''' using the '''for loop'''. | ||
+ | * Append the lines of a file to a '''list''' using the '''append method''' within the '''for loop'''. | ||
|- | |- | ||
Line 524: | Line 498: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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''' | ||
|- | |- | ||
Line 546: | Line 518: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| And the answers are, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| And the answers are, | ||
− | # '''open ''' | + | # '''open function''' returns a '''file object''' |
− | # '''splitlines() '''displays the | + | # '''splitlines() '''displays the data line by line as '''strings''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 581: | Line 551: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Previous slide | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Previous slide | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This is | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This is Trupti Kini from IIT Bombay (or FOSSEE, if you wish) signing off. |
Thank you. | Thank you. | ||
|} | |} |
Latest revision as of 12:09, 31 January 2018
Title of script: Getting started with files
Author: Trupti Kini
Keywords: IPython, files, read, write, append, mode, splitlines()
|
|
Show Slide | Hello Friends. Welcome to the tutorial on "Getting started with files". |
Show Slide
Objectives
|
At the end of this tutorial, you will learn to -
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show Slide:
Pre-requisite |
To practice this tutorial, you should know about
If not, see the pre-requisite Python tutorials on this website. |
Show slide
Open a file
syntax:
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().
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:
r- Read mode w- Write mode a -Appending mode r+ - Read and Write |
r- is for Read mode
w- is for Write mode a - represents Appending mode and r+ - for both Read and Write mode
|
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.
|
Text box on the screen as per the narration | The file pendulum.txt is available in the Code File link of this tutorial.
|
[Terminal]
Press Ctrl+Alt+T keys Type ipython3 |
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
Type %pylab and press Enter. |
Let us initialise the pylab package.
Let us clear the terminal. |
Open pendulum.txt | Let us open the file pendulum.txt. |
[IPython Terminal]
|
Type
By default, it is ‘r’. |
[IPython Terminal]
Type f |
Let us type f on the terminal to see what it is.
|
[IPython Terminal]
Highlight filename 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()
|
Type
pend equal to f dot read open and close parentheses.
|
[IPython Terminal]
Type print(pend) |
Now, let us see what pend contains, by typing
print inside parentheses pend.
|
Point to the data | We can see that pend has all the data of the file pendulum.txt. |
[IPython Terminal]
Type pendHighlight data with newline characters |
Type just pend to see more explicitly, what it contains.
|
[IPython Terminal]
|
Let us learn to split the variable pend into a list of lines in the file.
|
[IPython Terminal]
pend_list = pend.splitlines() |
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_list |
Type pend underscore list Press Enter |
Highlight the data into list of lines | We got the data into list of lines. |
Highlight the data into list of lines | pend_list does not contain newline characters like \n.
|
But, before that we will have to close the file, since the file has already been read till the end. | |
[IPython Terminal]
|
Let us close the file opened into f.
Type f dot close open and close parentheses Press Enter |
[IPython Terminal]
|
It is a good programming practice
|
Show Slide
Exercise 1
|
Pause the video here, try out the following exercise and then resume the video.
Recall that we have closed the file earlier. Let us switch back to the terminal. |
Let us now move on to reading files line- by - line. | |
[IPython Terminal]
Typef = open('pendulum.txt') |
To re-open the file again, type f is equal to open inside parentheses inside quotes pendulum.txt |
[IPython Terminal]
Type for line in f: print(line) |
Now, to read the file line-by-line, we iterate over the file object using the for loop.
Type for line in f colon press Enter four spaces print inside parentheses line |
[IPython Terminal]
|
Here, line is a loop variable, and it is not a keyword.
Press Enter twice. |
[IPython Terminal]
|
Instead of just printing the lines, let us append them into a list, say line_list. |
[IPython Terminal]
Type line_list = [] |
We first initialize a line_list as an empty-list
line underscore list is equal to open and close square brackets. Press Enter |
[IPython Terminal]
Type for line in open('pendulum.txt'): line_list.append(line)
|
Type the code as
four space line underscore list dot append inside parentheses line Press Enter twice.
|
We could, as usual close the file using f.close() and re-open it. | |
But, this time, let's leave the file object f and directly open the file within the for statement.
| |
[IPython Terminal]
Highlight data |
Let us see what line_list contains.
|
[IPython Terminal]
|
line underscore 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 -
|
Show Slide
Summary |
|
Show Slide
Assignment
|
Here are some self assessment questions for you to solve
1. The open function returns a
2. What does the function splitlines() do.
|
Show Slide
|
And the answers are,
|
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 |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Trupti Kini from IIT Bombay (or FOSSEE, if you wish) signing off.
Thank you. |