PYTHON
LESSON 5
CFGDEGREE → FOUNDATION MODULE
AGENDA
01 File handling
02 Reading and writing files
03 Converting dictionaries into csv files
04 Problem solving with Python
PYTHON FILE HANDLING
IMPORTANT POINTS # TYPES OF FILES IN PYTHON
● Imagine, we are creating a program that ● Binary file
works with a large amount of data
● We cannot expect that data to be stored in
a variable as the variables are volatile in ● Text file
nature.
● The data is stored externally, for example a
file
● As files are non-volatile in nature, the data
will be stored permanently and using
python we will handle these files in our
applications.
BINARY FILES IN PYTHON
IMPORTANT POINTS # EXAMPLE BINARY FILES
● Document files: .pdf, .doc, .xls etc.
● We can open some binary files in the ● Image files: .png, .jpg, .gif, .bmp etc.
normal text editor but we CANNOT ● Video files: .mp4, .3gp, .mkv, .avi etc.
read the content present inside the ● Audio files: .mp3, .wav, .mka, .aac etc.
file. ● Database files: .sql, .accde, .frm, .sqlite etc.
● Archive files: .zip, .rar, .iso, .7z etc.
● Executable files: .exe, .dll, .class etc.
● All the binary files will be ENCODED in
the binary format, which can be
understood only by a computer or
machine.
TEXT FILES IN PYTHON
IMPORTANT POINTS # TEXT FILES IN PYTHON
● Text files do not have any specific ● Web standards: html, XML, CSS, JSON etc.
encoding and it can be opened in ● Source code: c, app, js, py, java etc.
normal text editor itself. ● Documents: txt, tex, RTF etc.
● Tabular data: csv, tsv etc.
● Configuration: ini, cfg, reg etc.
FILE HANDLING OPERATIONS
DEFINITION # EXAMPLE CREATE & OPEN FILE
Most common
● open / read / write / close
file_object =
Other open(file_name, mode)
● rename / delete
Mode
● ‘r’ – Read Mode
● ‘w’ – Write Mode
● ‘a’ – Append Mode
● ‘r+’ – Read or Write Mode (same file)
● ‘a+’ – Append or Read Mode (same file)
for BINARY files use the same modes with the letter ‘b’ at the end.
'rb' or 'wb' and so on
READING FROM A FILE
DEFINITION # EXAMPLE
● In order to read a file in python, read()
we must open the file in read
readline()
mode.
readlines()
● There are three ways in which
we can read the files in python. What is the difference?
WRITE TO A FILE
DEFINITION # EXAMPLE
● we need to open or create a new file
● write a line or lines into the new file
See DEMO(s)
● once finished, then we must close the
file to save the changes
CONTEXT MANAGER
PYTHON DATA TYPE # EXAMPLE
● Context managers facilitate the with open('[Link]', 'w+') as text_file:
proper handling of resources.
people = 'Joanne \nSusan \nAmina'
text_file.write(people)
● In the case of performing file
operations a context manager ---------------------------------------------------------------------------
would automatically open and
close the file for us. with open('[Link]', 'r') as text_file:
contents = text_file.read()
● The syntax is slightly different, it print(contents)
uses the with word. We can call it
a with block
WORKING WITH CSV FILES
IN PYTHON # EXAMPLE
● csv is a Python library that
simplifies reading and writing
tabular text from/to a csv file. See DEMO(s) &
Complete EXERCISE(s)
● The library needs to be
imported first to be able to
access its functions and
objects.
PROBLEM SOLVING
IN PYTHON # EXAMPLE STEPS
Write a Python program to count the 1. Take the file name and the word to be
occurrences of a word in a text file counted from the user.
● Your program will take a word from 2. Read each line from the file and split
the user and count the number of the line to form a list of words.
occurrences of that word in a file. 3. Check if the word provided by the user
and any of the words in the list are
equal and if they are, then increment
the word count.
4. Exit
THANK YOU!