Chapter 5
File Handling in
Contents: Read/write operations & methods with files
Reading / Writing from/to a text file
Reading data from a CSV file
(c) Bharadwaj V Dept of ECE NUS (2023) 1
Types of File Formats
• File formats commonly used
(a) Text file: “mydata.txt”
(b) CSV (comma separated values) “mydata.csv”
(you can also open this with Excel)
(c) JSON – JavaScript Object Notation (basically text data;
You can open on browsers too)
(d) XLSx file
(e) Zip file
(c) Bharadwaj V Dept of ECE NUS (2023) 2
Text File Processing General Procedure
General procedure for reading/writing from/to the files –
3 step process
• File has to be “open” using its name (“mydata.txt”) –
use appropriate commands to open a file which returns
a file handle (a pointer that refers to the file under consideration)
• Then start reading/writing from/to the file
• After all the processing, you need to “close” the file
formally
(c) Bharadwaj V Dept of ECE NUS (2023) 3
Text File Processing Methods
Two main operations – Read & Write
Text information can be read
• All at once!
• Line-by-line
In Python – when we read all the data at once it
is stored as a list. This is applicable when the
data in the file is small
(c) Bharadwaj V Dept of ECE NUS (2023) 4
Text File Processing Methods
In Python – When we read the data one line at a
time for processing we can use the data in the way
we want. That is, we can store as a string and then
process or assign to a variable if it is a numerical
data, etc. We can do element-by-element
processing on a single data
This is applicable when the data in the file is large.
We have Python file processing methods to do this
easily.
(c) Bharadwaj V Dept of ECE NUS (2023) 5
Text File Writing/Reading methods
• Opening a file for processing
>> file = open('testfile.txt','w')
"file" is called as file handle; You can use any name; ‘w’ denotes
that you want to write to a file called “testfile.txt”.
• Writing into a file
>> file.write('1. Hello Class!\n')
>> file.write('1. This is our first file processing program.\n')
...
write() is a Python method which has to be used along with the
file handle you declared.
Tutorial 5.1 – Open a file as specified and write text
data and close. Check if the file sits in your folder.
(c) Bharadwaj V Dept of ECE NUS (2023) 6
Text File Writing/Reading methods
• Reading from a file
>> file = open('testfile.txt', 'r’) # note ‘r’
>> file.read()
read() is a Python method which has to be
used along with the file handle you
declared.
Tutorial 5.2 – Read the text data you
just wrote in Tut 5.1 and close. Print
the text you read.
(c) Bharadwaj V Dept of ECE NUS (2023) 7
Text File Writing/Reading methods
• Reading line by line from a file
>> file.readline()
readline() is a Python method which has to be used
along with the file handle you declared and it aids
reading line by line. This is useful for large data
processing.
Tutorial 5.3 – Read the text data you just
wrote in Tut 5.1 line by line using a loop,
measure its length, display, and close. Print
the text you read. What do you observe?
This is an important exercise.
(c) Bharadwaj V Dept of ECE NUS (2023) 8
Text File Writing/Reading methods
• Reading all the data at once from a file
>> readlines()
readlines() is a Python method which has to be
used along with the file handle you declared and
it aids reading all the data at once into a LIST.
This is useful for small data processing.
Tutorial 5.4 – Create a text file with a
few words and then read all the words
using readlines() method. What do you
observe?
(c) Bharadwaj V Dept of ECE NUS (2023) 9
Text File Writing/Reading methods
• Writing data into a file
We may need to write our outputs generated by
our programs frequently into a file.
Tutorial 5.5 – Generate 10 random
numbers and write them into a text
file along with a meaningful text
message and then read all the
numbers into a list. Print your list.
(c) Bharadwaj V Dept of ECE NUS (2023) 10
Text File Writing/Reading methods
with() method for reading/writing
• with open('testfile.txt', 'r') as file: # file is your file
handle
Very clean and convenient syntax!
You don't need to bother about closing a file every
time!
Tutorial 5.6 – Demonstrate with() method
by reading the data from testfile.txt you
created in Tutorial 5.1. Then measure the
length of each line and write it into a text
file along with a meaningful text message.
(c) Bharadwaj V Dept of ECE NUS (2023) 11
Text File Writing/Reading methods
Tutorial 5.7 – Writing and reading lists to/from a file
(a) Consider a list of string elements. Write this list into
a text file. Read the file and print the list
(b) Generate a random list comprising a set of integers.
Write this list into a text file. Read the file and print the
integers as a list.
What is your observation?
(c) Bharadwaj V Dept of ECE NUS (2023) 12
Other useful functions during file data processing
• iter() – This method converts an iterable to an iterator
object. An iterator object is one which when called
returns the next item in an iterable.
This is useful when we do not want to keep track of
elements in an iterable and when we use next() method
a StopIteration (an exception signal) will occur when there are
no more elements!
Quite useful when we handle large iterables and when
we do not wish to write separate loops to access
elements!
(c) Bharadwaj V Dept of ECE NUS (2023) 13
Other useful functions during file data processing
• next(iterator object) – This method when called
returns the next element in the iterable.
Tutorial 5.8(a) – Create a list, convert to an iterator
object and show how elements can be accessed from a
function by passing an iterator to the function and you
need to allow access to the list of elements only in even
iterations, starting from i=0 onwards.
(c) Bharadwaj V Dept of ECE NUS (2023) 14
Other useful functions during file data processing
• map(function, iterable): This method returns a map
object (which is essentially an iterator) of the results
after mapping or applying the given function to each
item of a given iterable.
Thus, the map object created can be used to generate
the result of the function on all the elements of the
iterable in one step!
So, if you wish to apply the same function operation on
all the elements of an iterable, you can use this method!
(c) Bharadwaj V Dept of ECE NUS (2023) 15
Other useful functions during file data processing
Tutorial 5.8(b) – Consider a tuple and divide all the
elements of the tuple by 2 and store it as a separate list.
DIY! Create a list of integers as strings, convert them to
integers!
(c) Bharadwaj V Dept of ECE NUS (2023) 16
Other useful functions during file data processing
• lambda() – This is an anonymous function! This means that the
function is without a name! Recall we have used “def” to define
a function with a name.
Similarly, lambda() is used to define a function without a name and hence there
is a constraint that these lambda() functions cannot be lengthy!
Note:
• They are restricted to a single expression
• They can have any number of arguments but only one expression, which is
evaluated and returned
Example: (Run and see this!)
>> cube = lambda x: x*x*x # lambda args: single expression
>> print(type(cube)) # what will this return?
>> print(cube(10))
(c) Bharadwaj V Dept of ECE NUS (2023) 17
Other useful functions during file data processing
• filter() – You can use this function to filter out the
desired results during processing; See the tutorial
problem below.
Tutorial 5.8(c) – Generate a random list of integers.
Write a lambda() to determine the even numbers in your
list and filter all such even numbers from your list and
display.
Tutorial 5.8(d) – Use of map() & lambda function!
Create a list and cube each element. Use a lambda
function to define your operation and use a map() to
perform on all elements.
(c) Bharadwaj V Dept of ECE NUS (2023) 18
Other useful functions during file data processing
Tutorial 5.8(e) – Use of multiple lambda functions! Create
a list of lists and determine the max in each list. Add all the
max values and display.
- 1st lambda to do a sorting each sublist
- 2nd lambda to find max from each sublist
- 3rd lambda to add all the elements of the result from the
above step
Note that there are two functions in python for sorting - sort() and
sorted() – Note the difference!
- sorted(L) – takes a list and gives a list L’ which is a sorted list and
original L is not affected;
- sort(): L.sort() also sorts but affects the original list
(c) Bharadwaj V Dept of ECE NUS (2023) 19