diff -r e675f9208b91 -r 4054b1a6392d getting-started-files/script.rst --- a/getting-started-files/script.rst Wed Oct 13 17:32:23 2010 +0530 +++ b/getting-started-files/script.rst Wed Oct 13 17:32:59 2010 +0530 @@ -1,10 +1,30 @@ -======== - Script -======== +.. Objectives +.. ---------- + +.. By the end of this tutorial, you will be able to +.. 1. Open and read the contents of a file. +.. #. Read files line by line. +.. #. Read all the contents of the file at once. +.. #. Close open files. + +.. Prerequisites +.. ------------- -Welcome to the tutorial on getting started with files. +.. 1. getting started with ipython +.. #. getting started with lists +.. #. getting started with for + +.. Author : Puneeth + Internal Reviewer : + External Reviewer : + Checklist OK? : [2010-10-05] -{{{ Screen shows welcome slide }}} +Script +------ + +{{{ Show the slide containing title }}} + +Hello Friends. Welcome to the tutorial on getting started with files. {{{ Show the outline for this tutorial }}} @@ -33,7 +53,8 @@ f The file object shows, the file which is open and the mode (read -or write) in which it is open. +or write) in which it is open. Notice that it is open in read only +mode, here. We shall first learn to read the whole file into a single variable. Later, we shall look at reading it line-by-line. We use @@ -54,24 +75,29 @@ pend -%%1%% Pause the video here and split the variable into a list, -``pend_list``, of the lines in the file and then resume the -video. Hint, use the tab command to see what methods the string -variable has. +Following is an exercise that you must do. + +%%1%% Split the variable into a list, ``pend_list``, of the lines in +the file. Hint, use the tab command to see what methods the string +variable has. -#[punch: should this even be put? add dependency to strings LO, -where we mention that strings have methods for manipulation. hint: -use splitlines()] +Please, pause the video here. Do the exercise and then continue. + +.. #[punch: should this even be put? add dependency to strings LO, +.. where we mention that strings have methods for manipulation. hint: +.. use splitlines()] + :: pend_list = pend.splitlines() pend_list -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. -#[punch: should we mention file-pointer?] +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. + +.. #[punch: should we mention file-pointer?] Let us close the file opened into f. :: @@ -89,8 +115,11 @@ Let us, now move on to reading files line-by-line. -%%1%% Pause the video here and re-open the file ``pendulum.txt`` -with ``f`` as the file object, and then resume the video. +Following is an exercise that you must do. + +%%2%% Re-open the file ``pendulum.txt`` with ``f`` as the file object. + +Please, pause the video here. Do the exercise and then continue. We just use the up arrow until we reach the open command and issue it again. @@ -123,8 +152,10 @@ statement. This will save us the trouble of closing the file, each time we open it. -for line in open('/home/fossee/pendulum.txt'): -line_list.append(line) +:: + + for line in open('/home/fossee/pendulum.txt'): + line_list.append(line) Let us see what ``line_list`` contains. :: @@ -143,5 +174,11 @@ a whole, using the read command or reading it line by line by iterating over the file object. -Thank you! +{{{ Show the "sponsored by FOSSEE" slide }}} + +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +Hope you have enjoyed and found it useful. +Thank you! + +