Python-3.4.3/C2/Getting-started-with-files/English
'Python/C2/Getting-started-with-files/English'Title of script: Getting started with files
Author: Trupti Kini
Keywords: Python, IPython, files,
|
|
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 -
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show Slide:
Pre-requisite |
To practice this tutorial, you should know about how to-
If not, see the pre-requisite Python tutorials on http://spoken-tutorial.org |
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().
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- represent the 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.
|
The file pendulum.txt is available in the code file link of this tutorial.
| |
[Terminal]
ipython3
|
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package.
|
Let us open the file, pendulum.txt.
| |
[IPython Terminal]
|
Type
|
[IPython Terminal]
f
|
Let us type f on the terminal to see what it is.
|
[IPython Terminal]
Highlight filepath and mode 'r' |
'r' stands for read only mode.
|
[IPython Terminal]
|
Now let us learn to read the whole file into a single variable.
|
type
pend = f.read()
|
Type
|
[IPython Terminal]
|
Now, let us see what pend contains, by typing
print inside parentheses pend.
|
Point to the output | We can see that pend has all the data of the file pendulum .txt.
|
[IPython Terminal]
|
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() pend_list
|
For this we need to store this list in a variable, say pend_list
pend_list equal to pend dot splitlines open and close parentheses Press Enter
pend underscore list Press Enter
|
Highlight output | pend_list does not contain newline characters like \n.
|
Now, let us learn to read the file line-by-line. | |
[IPython Terminal]
|
But, before that we will have to close the file, since the file has already been read till the end.
Type f dot close open and close parentheses |
[IPython Terminal]
|
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.
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]
print(line)
|
Now, to read the file line-by-line, we iterate over the file object line-by-line, using the for loop.
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.
|
[IPython Terminal]
|
Instead of just printing the lines, let us append them to a list, say line_list. |
[IPython Terminal]
|
We first initialize an line_list as an empty-list
line underscore list equal to open and close square brackets. |
[IPython Terminal]
line_list.append(line)
|
Type the code as
four space line underscore list dot append inside parentheses line
|
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.
| |
[IPython Terminal]
Highlight output |
Let us see what line_list contains.
|
[IPython Terminal]
|
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 -
|
Show Slide
Summary |
# Read the data in the files line by line by iterating over the file object using the for loop.
|
Show Slide
Assignment
|
Here are some self assessment questions for you to solve
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 _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
Thank you. |