5 File Handling 1
5 File Handling 1
NOIDA
E-CONTENT FOR CLASS XII (COMPUTER SCIENCE,
083)
CHAPTER : FILE HANDLING IN PYTHON
Prepared By,
Geethanjali B K
PGT IT
JNV East Godavari
I
LEARNING OBJECTIVES
At the end of this chapter you will be able to learn :
❏ Understanding Files
❏ Types of Files
❏ Understanding Text Files
❏ Opening and closing Text files
❏ Reading and writing in Files
❏ Understanding Binary Files
❏ Pickling and Unpickling
❏ Opening and closing Binary files
❏ Reading and writing in Binary Files
❏ CSV (Comma separated values) files. 2
NEED FOR DATA FILE
HANDLING
● Mostly, in programming languages, all the values or data are stored in
some variables which are volatile in nature.
● Because data will be stored into those variables during run-time only
and will be lost once the program execution is completed. Hence it is
better to save these data permanently using files.
3
INTRODUCTION
● A file in itself is a sequence of bytes stored in some storage device like
hard-disk, pen-drive etc.
● Python allow us to create and manage three types of files :
1. TEXT FILE
2. BINARY FILE
4
TEXT FILE
● A text file is structured as a sequence of lines.
● Line is a sequence of characters (ASCII or UNICODE)
● Stores information in ASCII or Unicode characters.
● Each line of text is terminated by a special character known as End Of
Line character.
● Text files are stored in human readable form and they can also be
created using any text editor.
5
BINARY FILE
● A file that contains information in the same format in which
information is held in memory.
● Binary file contains arbitrary binary data.
● So when we work on binary file, we have to interpret the raw
bit pattern(s) read from the file into correct type of data in
our program.
● Python provides special module(s) for encoding and
decoding of data for binary file.
6
CSV FILES
● CSV stands for Comma Separated Values.
● CSV is just like a text file, in a human readable format which is
extensively used to store tabular data, in a spreadsheet or
database.
● The separator character of CSV files is called a delimiter.
● Default delimiter is comma (,). Other delimiters are tab (\t), colon
(:), pipe (|), semicolon (;) characters.
7
8
STEPS TO PROCESS A FILE
1. Determine the type of file usage.
a. Reading purpose : If the data is to be brought in from a file to
memory
b. Writing purpose : If the data is to be sent from memory to file.
2. Open the file and assign its reference to a file object or file-handle.
3. Process the file as required : Perform the desired operation from
the file.
4. Close the file.
9
OPENING A TEXT FILE
● The key function for working with files in Python is the open()
function.
● It accepts two parameters : filename, and mode.
Syntax:
<file_object_name> = open(<file_name>,<mode>)
Example: f = open(“demo.txt”,”r”)
11
Mode ●
Description Various Modes for opening a text file are:
“r” Read Default value. Opens a file for reading, error if the
file does not exist.
“w” Write Opens a file for writing, creates the file if it does not
exist
“a” Append Opens a file for appending, creates the file if it does
not exist
“w+” Write and File is created if it does not exist.If the file exists
Read past data is lost (truncated).Both reading and
writing operations can take place.
“a+” Append and File is created if it does not exist.If the file exists 12
Read past data is not lost .Both reading and
CLOSING A FILE
● close()- 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.
● <fileobject>. close() will be used to close the file object, once we
have finished working on it.
Syntax:
<fileObject>.close()
Example : f.close()
14
read() METHOD
● By default the read() method returns the whole text, but you can
also specify how many characters you want to return by passing the
size as argument.
Syntax: <file_object>.read([n])
f = open("demo.txt", "r")
print(f.read(15))
15
Returns the 15 first characters of the file "demo.txt".
16
readline() METHOD
Syntax:
<file_object>.readline()
● Example
f = open("demo.txt", "r")
print(f.readline())
● This example program will return the first line in the file
17
“demo.txt” irrespective of number of lines in the text file.
Reading a complete file line by line using
readline().
18
readlines() METHOD
● readlines() method will return a list of strings, each separated by
\n
● readlines() can be used to read the entire content of the file.
Syntax:
<file_object>.readlines()
print(f.readlines()) 19
Read a file using Readlines()
20
21
WRITING TO A TEXT FILE
● A Program writes into a text/binary file in hard disk.
● Followings are the methods to write a data to the file.
○ write () METHOD
○ writelines() METHOD
● To write to an existing file, you must add a parameter to the open()
22
write() Method
● write() method takes a string ( as parameter ) and writes it in the file.
● For storing data with end of line character, you will have to add \n
character to end of the string
● Example:
f = open("demo_write.txt", ""a)
f.close()
print(f.read())
24
writelines() METHOD
● For writing a string at a time, we use write() method, it can't
be used
25
26
The flush()
The flush function forces the writing of data on disc still
pending on the output buffer.
Syntax : <file_object>.flush()
27
28
Standard Input, Output, and Error
Streams
❏ Keyboard is the standard input device
❏ stdin - reads from the keyboard
❏ Monitor is the standard output device.
❏ stdout - prints to the display
❏ If any error occurs it is also displayed on the monitor and
hence it is also the standard error device.
❏ Same as stdout but normally only for errors (stderr)
understand.
30
● Objects have a specific structure which must be maintained while
storing or accessing them.
● Python provides a special module called pickle module for this.
32
PICKLE Module
import pickle
33
Opening and closing binary files
Opening a binary file:
EXAMPLE :
f = open(“demo.dat”,”rb”)
f.close()
34
Mode Description● Various Modes for opening a binary file are:
“rb” Read Default value. Opens a file for reading, error if the file
does not exist.
“wb” Write Opens a file for writing, creates the file if it does not
exist
“ab” Append Opens a file for appending, creates the file if it does
not exist
“r+b”or Read and File must exist otherwise error is raised.Both reading
“rb+” Write and writing operations can take place.
“w+b”or Write and File is created if it does not exist.If the file exists past
“wb+” Read data is lost (truncated).Both reading and writing
operations can take place.
“a+b” or Append and File is created if it does not exist.If the file exists past35
“ab+” Read data is not lost .Both reading and writing operations
pickle.dump() Method
● pickle.dump() method is used to write the object in file
which is opened in binary access mode.
Syntax :
pickle.dump(<structure>,<FileObject>)
36
binary_file.dat file
after execution of
the program.
37
pickle.load() Method
● pickle.load() method is used to read data from a file
Syntax :
<structure> = pickle.load(<FileObject>)
38
39
Write a method to write employee
details into a binary file. Take the
input from the user.
Employee Details:
Employee Name
Employee Number
Department
Salary
40
Random Access in Files : tell() and
seek()
❏ tell() function is used to obtain the current position of the file
pointer
Syntax : f.tell()
Syntax : f.seek(offset,reference_point)
import csv
43
Opening / Closing a CSV File
● Open a CSV File :
f = open(“demo_csv.csv”,”r”)
OR
f = open(“demo_csv.csv”,”w”)
f.close()
44
Role of argument newline in
opening of csv files :
● newline argument specifies how would python handle new line
characters while working with csv files on different Operating
System.
● Additional optional argument as newline = “”(null string) with the
file open() will ensure that no translation of EOL character takes
place.
45
Steps to write data to a csv file
1. Import csv module
import csv
f = open(“csv_demo.csv”,”w”)
demo_writer = csv.writer(f)
● writerow() :
Syntax : <writer_object>.writerow()
● writerows() :
Syntax : <writer_object>.writerow() 47
Writing in CSV Files
● To write data into csv files, writer() function of csv module
is used.
● csv.writer():
○ Returns a writer object which writes data into writer
object.
● <writer_object>.writerows()
49
50
Reading from CSV Module
● To read data from csv files, reader function of csv module is used.
● csv.reader() :
import csv
f = open(“csv_demo.csv”,”r”)
demo_reader = csv.reader(f)
55
THANK YOU
56