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

From Script | Spoken-Tutorial
Revision as of 12:58, 29 October 2013 by Krupali (Talk | contribs)

Jump to: navigation, search
Timing Narration
0:00 હેલો મિત્રો, "ફાઈલો સાથે શરૂઆત" કરવા પરના ટ્યુટોરીયલમાં સ્વાગત છે.
0:08 આ ટ્યુટોરીયલના અંતે, તમે નીચે આપેલ કરવા માટે સમક્ષ હશો,
  1. ફાઈલ ખોલવું.
  2. ફાઇલના કન્ટેનટ્સ લાઈન થી લાઈન વાંચો.
  3. એક જ સમયે ફાઇલના સમગ્ર કન્ટેનટ્સ વાંચો.
  4. ફાઇલ ની લાઈન લીસ્ટમાં ઉમેરો.
  5. ફાઈલ બંધ કરો.
0:24 આ ટ્યુટોરીયલ શરૂ કરો તે પહેલાં, અમે તમને "Getting started with Lists" અને "Getting started with For" પરના ટ્યુટોરીયલ જોવા માટે સૂચિત કરીશું.
0:34 તો ટર્મિનલ ખોલો અને ipython શરુ કરો.
0:37 તો ટાઇપ કરો, ipython સ્પેસ હાયફન pylab.
0:46 ચાલો પ્રથમ pendulum dot txt ફાઈલ ખોલીએ, જે slash home slash fossee slash માં હાજર છે.
0:54 તો ટાઇપ કરો, f ઇકવલ ટુ open કૌશ અંદર અને સિંગલ અવતરણ ચિહ્નમાં slash home slash fossee slash pendulum dot txt.
1:11 અહીં f એ ફાઈલ ઓબ્જેક્ટ તરીકે ઓળખાય છે.
1:14 ચાલો f શું છે તે જાણવા માટે ટર્મિનલ પર f ટાઇપ કરીએ.
1:17 તો f ટાઇપ કરો અને એન્ટર ડબાઓ.
1:22 ફાઈલ ઓબ્જેક્ટ ફાઈલ પાથ અને ફાઈલ નો મોડ દર્શાવે છે જે open છે.
1:27 'r' stand for read only mode and 'w' stands for write mode.
1:32 As you can see, this file is open in read only mode.
1:40 We shall first learn to read the whole file into a single variable.
1:47 We use the read method to read all the contents of the file into the variable,pend.
1:53 So type pend is equal to f dot read closing brackets and hit Enter.
2:02 Now, let us see what pend contains, by typing Print space pend
2:11 We can see that pend has all the data of the file.
2:15 Type just pend to see more explicitly, what it contains.
2:25 So now, Pause the video here, try out the following exercise and resume the video.
2:30 Split the variable into a list, pend underscore list, of the lines in the file.
2:40 We use the function split lines to solve this problem.
2:44 So type pend underscore list is equal to pend dot split lines closing brackets and hit Enter.
3:05 Now, let us learn to read the file line-by-line.
3:11 But, before that we will have to close the file, since the file has already been read till the end.
3:19 Let us close the file opened into f.
3:24 Then type f dot close closing brackets and hit Enter.
3:29 Again type f on the prompt to see what it contains.
3:37 Notice, that it now says the file has been closed.
3:42 It is a good programming practice to close any file objects that we have opened, after their job is done.
3:50 Let us, now move on to reading files line-by-line.
3:54 Pause the video here, try out the following exercise and resume the video.
4:00 Re-open the file pendulum dot txt with f as the file object.
4:05 We just use the up arrow until we reach the open command and issue it again.Then hit Enter.
4:18 Now, to read the file line-by-line, we iterate over the file object line-by-line, using the for command.
4:27 Let us iterate over the file line-wise and print each of the lines.
4:35 So type in the command for space line space in space f colon , then , print line.
4:47 line is a variable, sometimes called the loop variable, and it is not a keyword.
4:53 We could have used any other variable name, but line seems meaningful enough.
5:00 Instead of just printing the lines, let us append them to a list, line underscore list .


5:07 We first initialize an empty list, line underscore list.
5:12 for that type line underscore list is equal to square bracket and hit Enter.
5:22 Let us then read the file line-by-line and then append each of the lines to the list.
5:30 We could, as usual close the file using f.close and re-open it.
5:36 But, this time, let's leave alone the file object f and directly open the file within the for statement.
5:43 This will save us the trouble of closing the file, each time we open it.
5:49 So type for line in open within brackets and single quotes slash home slash fossee slash pendulum dot txt colon
   line underscore list dot append within brackets line,Hit Enter.


6:22 Let us see what line underscore list contains.
6:26 so type line underscore list and hit Enter.
6:33 Notice that line_list is a list of the lines in the file, along with the newline characters.
6:42 If you noticed, pend underscore list did not contain the newline characters, because the string pend was split on the newline characters.
6:52 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.
7:04 So now, This brings us to the end of this tutorial. Lets revise what we have learnt,
7:12 1. Open and close files using the open and close functions respectively.
7:17 2. Read the data in the files as a whole,by using the read function.
7:22 3. Read the data in the files line by line by iterating over the file object using the for loop.
7:31 and finally Append the lines of a file to a list using the append function within the for loop.
7:38 Here are some self assessment questions for you
7:42 1. The open function returns a
7:46 string
7:48 list
7:49 file object
7:50 function
7:52 2. What does the function splitlines() do.
7:57 Displays the data as strings,all in a line
8:01 Displays the data line by line as strings
8:03 Displays the data line by line but not as strings
8:07 So now,let us look at the answers,
8:09 1.The function open , returns a file object.
8:15 2. The function splitlines displays the data line by line as strings.


8:21 So we hope you have enjoyed this tutorial and found it useful.
8:27 Thank you!

Contributors and Content Editors

Krupali