Data file handling in python introduction,opening & closing files
1.
2. File handling: Need for a data file, Types of file: Text files, Binary files
and CSV (Comma separated values) files.
● Text File: Basic operations on a text file: Open (filename – absolute
or relative path, mode) / Close a text file, Reading and Manipulation
of data from a text file, Appending data into a text file, standard input /
output and error streams, relative and absolute paths.
● Binary File: Basic operations on a binary file: Open (filename –
absolute or relative path, mode) / Close a binary file, Pickle Module –
methods load and dump; Read, Write/Create, Search, Append and
Update operations in a binary file.
● CSV File: Import csv module, functions – Open / Close a csv file,
Read from a csv file and Write into a csv file using csv.reader ( ) and
csv.writerow( ).
CONTENTS AS PER NEW CBSE CURRICULUM
3. File handling: Need for a data file, Types of file: Text files,
Binary files & CSV Files
● Text File: Basic operations on a text file: Open (filename –
absolute or relative path, mode) / Close a text file.
CONTENTS COVERAGE IN THIS PRESENTATION
4. Till now whatever programs we have written, the standard input in coming from keyboard and output
is going to monitor i.e. no where data is stored permanent and entered data is present as long as
program is running . After that execution, the programmatically generated data is disappeared. And
when we again run those programs we start with a fresh data.
Why? This is because that data is entered in RAM which is temporary memory and its data is
volatile.
However, if we need to store the data, we may store it onto the permanent storage which is
not volatile and can be accessed every time.. Here, comes the need of file.
Files enables us to create, update, read, and delete the data stored through our python program.
And all these operations are managed through the file systems.
WHY DO WE NEED FILES
Hard
Disk
Program in RAM
(Random Access
Memory)
5. A file (i.e. data file) is a named place on the disk where a
sequence of related data is stored.
In python files are simply stream of data, so the
structure of data is not stored in the file, along with data.
It contains data pertaining to a specific application, for
later use. The data files can be stored in two It It contains data
pertaining to a specific application, for later use.
2
What is a file in python
6. TYPES OF FILES
Python allow us to create and manage
three types of file
1. TEXT FILE
2. BINARY FILE
3. CSV FILE
7. What is Text File?
Text file stores information in ASCII
OR UNICODE character. In text file everything
will be stored as a character for example if
data is “computer” then it will take 8 bytes
and if the data is floating value like 11237.9876 it
will take 10 bytes.
In text file each like is terminated by special
character called EOL. In text file some
translation takes place when this EOL character is
read or written. In python EOL is ‘n’ or ‘r’ or
combination of both
1. TEXT FILE
8. What is Binary File?
It stores the information in the same format
as in the memory i.e. data is stored according to its
data type so no translation occurs.
In binary file there is no delimiter for a new line
Binary files are faster and easier
for a program to read and write than text
files.
Data in binary files cannot be directly read, it can
be read only through python program for
2. BINARY FILE
9. What is CSV File?
CSV is a simple file format used to store tabular
data, such as a spreadsheet or database.
Files in the CSV format can be easily imported to
and exported from programs that store data in
tabular format.
3. CSV Files
10. TEXT FILES BINARY FILES CSV FILES
1. Text Files are sequential files 1. A Binary file contain arbitrary binary
data
CSV is a simple file format used to
store tabular data.
2. Text files only stores texts 2 Binary Files are used to store binary
data such as image, video, audio, text
CSV are used to stores data such as a
spreadsheet or database
3. There is a delimiter EOL (End of Line
i.e n)
3. There is no delimiter A comma-separated values file is a
delimited text file that uses a comma
to separate values
4. Due to delimiter text files takes
more time to process. while reading or
writing operations are performed on
file.
4. No presence of delimiter makes files
to process fast while reading or writing
operations are performed on file.
CSV is faster to handle as these are
smaller in size. Moreover CSV is easy
to generate
5. Text files easy to understand
because these files are in human
readable form
5. Binary files are difficult to understand. CSV is human readable and easy to
read manually
Difference Between Text Files, Binary Files & CSV Files
12. OPENING AND CLOSING FILES
To handle data files in python, we
need to have a file object. Object can be
created by using open() function or file()
function.
To work on file, first thing we do is open
it. This is done by using built in function
open().
Syntax of open() function is
file_object = open(filename [, access_mode]
[,buffering])
13. OPENING AND CLOSING FILES
open() requires three arguments to work,
first one ( filename ) is the name of the file on secondary
storage media, which can be string constant or a variable.
The name can include the description of path, in case, the
file does not reside in the same folder / directory in which
we are working
The second parameter (access_mode) describes
how file will be used throughout the program. This is an
optional parameter and the default access_mode is
reading.
14. OPENING AND CLOSING FILES
The third parameter (buffering) is for specifying how much is
read from the file in one read.
When we work with file(s), a buffer (area in memory where
data is temporarily stored before being written to file), is
automatically associated with file when we open the file.
While writing the content in the file, first it goes to buffer and
once the buffer is full, data is written to the file. Also when file is
closed, any unsaved data is transferred to file. flush() function is
used to force transfer of data from buffer to file
15. OPENING FILE
myfile = open(“story.txt”)
here disk file “story.txt”is loaded in the memory and
its reference is linked to “myfile” object, now python
program will access “story.txt” through “myfile”
object.
here “story.txt” is present in the same folder where
.py file is stored otherwise if disk file to work is in
another folder we have to give full path.
16. myfile = open(“d:mydatapoem.txt”,”r”)
here we are accessing “poem.txt” file stored in
separate location i.e. d:mydata folder.
at the time of giving path of file we must use double backslash() in place of
single backslash because in python single slash is used for escape
character and it may cause problem like if the folder name is “nitin” and we
provide path as d:nitinpoem.txt then in nitin “n” will become escape
character for new line, SO ALWAYS USE DOUBLE BACKSLASH IN
PATH
Another solution of double backslash is using “r” before the path making
the string as raw string i.e. no special meaning attached to any character
as:
myfile = open(r“d:mydatapoem.txt”,”r”)
OPENING FILE
17. Absolute Vs Relative PATH
To understand PATH we must be familiar
with the terms: DRIVE,
FOLDER/DIRECTORY, FILES.
Our hard disk is logically divided into many
parts called DRIVES like C DRIVE, D DRIVE
etc.
18. Absolute Vs Relative PATH
The drive is the main container in which we put
everything to store.
The naming format is : DRIVE_LETTER:
For e.g. C: , D:
Drive is also known as ROOT DIRECTORY.
Drive contains Folder and Files.
Folder contains sub-folders or files
Files are the actual data container.
21. Absolute Path
Absolute path is the full address of any file or
folder from the Drive i.e. from ROOT
FOLDER. It is like:
Drive_Name:FolderFolder…filename
For e.g. the Absolute path
REVENUE.TXT will be
C:SALES2018REVENUE.TXT
Absolute path of SEC_12.PPT is
C:PRODNOIDASec_12.ppt
of file
22. Relative Path
Relative Path is the location of file/folder
from the current folder. To use Relative
path special symbols are:
Single Dot ( . ) : single dot ( . ) refers to current
folder.
Double Dot ( .. ) : double dot ( .. ) refers to
parent
folder
Backslash ( ) : first backslash before (.)
and double dot( .. ) refers to ROOT folder.
27. FILE ACCESS MODES
MODE File Opens in
r Text File Read Mode
rb Binary File Read Mode
These are the default modes. The file
pointer is placed at the beginning for reading
purpose, when we open a file in this mode.
28. FILE ACCESS MODES
MODE File Opens in
r+ Text File Read & Write Mode
rb+ Binary File Read Write Mode
w Text file write mode
wb Text and Binary File Write Mode
w+ Text File Read and Write Mode
wb+ Text and Binary File Read and Write
Mode
a Appends text file at the end of file, if file
does not exists it creates the file.
29. FILE ACCESS MODES
MODE File Opens in
ab Appends both text and binary files at
the end of file, if file does not exists it
creates the file.
a+ Text file appending and reading.
ab+ Text and Binary file for appending and
reading.
30. FILE ACCESS MODES - EXAMPLE
For Ex:
f=open(“notes.txt”, ‘r’)
This is the default mode for
a file.
notes.txt is a text file and is
opened in read mode only.
31. FILE ACCESS MODES - EXAMPLE
For Ex:
f=open(“notes.txt”, ‘r+’)
notes.txt is a text file and is
opened in read and write mode.
32. FILE ACCESS MODES - EXAMPLE
For Ex:
f=open(“tests.dat ”, ‘rb’)
tests.dat is a binary file and
is opened in read only mode.
33. FILE ACCESS MODES - EXAMPLE
For Ex:
f=open(“tests.dat”, ‘rb+’)
tests.dat is binary file and is
opened in both modes that is
reading and writing.
34. FILE ACCESS MODES - EXAMPLE
For Ex:
f=open(“tests.dat”, ‘ab+’)
tests.dat is binary file and is
opened in both modes that is
reading and appending.
36. close FUNCTION
fileobject. close() will be used to close
the file object, once we have finished
working on it. The method will free up all the
system resources used by the file, this means
that once file is closed, we will not be able to
use the file object any more.
For example:
f.close()
37. THANK YOU &
HAVE A NICE DAY
UNDER THE GUIDANCE OF KVS RO AGRA
VEDIO LESSON PREPARED BY:
KIRTI GUPTA
PGT(CS)
KV NTPC DADRI