SlideShare una empresa de Scribd logo
1 de 158
CHAPTER - 08
DATA FILE HANDLING
Unit I
Programming and Computational
Thinking (PCT-2)
(80 Theory + 70 Practical)
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)
Department of Computer Science, Sainik School Amaravathinagar
Cell No: 9431453730
Praveen M Jigajinni
Prepared by
Courtesy CBSE
Class XII
LEARNING OUTCOMES
LEARNING OUTCOMES
After going through the chapter, student
will be able to:
Understand the importance of data file for
permanent storage of data.
Understand how standard Input/Output
function work.
Distinguish between text and binary file
Open and close a file ( text and binary)
Read and write data in file
Write programs that manipulate data file(s
INTRODUCTION – DATA FILES
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.
BASIC OPERATIONS ON FILE
Basic operations performed on a data file are:
1. Naming a file
2. Opening a file
3. Reading data from the file
4. Writing data in the file
5. Closing a file
BASIC OPERATIONS ON FILE
FILE PROCESSING
Contd.. next
Using these basic operations, we can process file
in many ways, such as
1. CREATING A FILE
2. TRAVERSING A FILE FOR DISPLAYING THE DATA ON SCREEN
3. APPENDING DATA IN FILE
4. INSERTING DATA IN FILE
Contd.. next
FILE PROCESSING
INTRODUCTION – DATA FILES
6. CREATE A COPY OF FILE
7. UPDATING DATA IN THE FILE … etc
5. DELETING DATA FROM FILE
TYPES OF FILES
TYPES OF FILES
Python allow us to create and manage two
types of file
1. TEXT FILE
2. BINARY FILE
What is Text File?
A text file is usually considered as sequence
of lines. Line is a sequence of characters (ASCII or
UNICODE), stored on permanent storage media.
The default character coding in python is ASCII
each line is terminated by a special character,
known as End of Line (EOL). At the lowest level,
text file will be collection of bytes. Text files are
stored in human readable form and they can also
be created using any text editor.
1. TEXT FILE
What is Binary File?
A binary file contains arbitrary binary data
i.e. numbers stored in the file, can be used for
numerical operation(s). 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. In the case of binary file it is
extremely important that we interpret the correct
data type while reading the file. Python provides
special module(s) for encoding and decoding of
data for binary file.
2. BINARY FILE
OPENING AND CLOSING FILES
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])
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.
OPENING AND CLOSING FILES
The third parameter (buffering) is for
specifying how much is read from the file in one
read.
Finally, The function will return an object
of file type using which we will manipulate the
file, in our program.
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.
OPENING AND CLOSING FILES
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
FILE ACCESS MODES
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.
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.
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.
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.
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.
FILE ACCESS MODES - EXAMPLE
For Ex:
f=open(“tests.dat ”, ‘rb’)
tests.dat is a binary file and is
opened in read only mode.
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.
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.
close FUNCTION
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()
FILE READING METHODS
FILE READING METHODS
PYTHON
PROGRAM
A Program reads a text/binary file from
hard disk. File acts like an input to a program.
FILE READING METHODS
Followings are the methods to read a
data from the file.
1. readline() METHOD
2. readlines() METHOD
3. read() METHOD
readline() METHOD
readline() will return a line read, as a
string from the file. First call to function will
return first line, second call next line and so on.
It's syntax is,
fileobject.readline()
readline() EXAMPLE
First create a text file and save under
filename notes.txt
readline() EXAMPLE
readline() EXAMPLE O/P
readline() will return only one line from a file,
but notes.txt file containing three lines of text
readlines() METHOD
readlines()can be used to read the entire
content of the file. You need to be careful while
using it w.r.t. size of memory required before
using the function. The method will return a list
of strings, each separated by n. An example of
reading entire data of file in list is:
It's syntax is,
fileobject.readlines()
as it returns a list, which can then be used for
manipulation.
readlines() EXAMPLE
readlines() EXAMPLE
The readlines() method will return a list
of strings, each separated by n
read() METHOD
The read() method is used to read entire
file
The syntax is:
fileobject.read()
For example
Contd…
read() METHOD EXAMPLE
read() METHOD EXAMPLE O/P
The read() method will return entire file.
read(size) METHOD
read(size) METHOD
read() can be used to read specific size
string from file. This function also returns a
string read from the file.
Syntax of read() function is:
fileobject.read([size])
For Example:
f.read(1) will read single
byte or character from a file.
read(size) METHOD EXAMPLE
f.read(1) will read single byte or character from
a file and assigns to x.
read(size) METHOD EXAMPLE O/P
WRITING IN TO FILE
WRITING METHODS
PYTHON
PROGRAM
A Program writes into a text/binary file
from hard disk.
WRITING METHODS
1. write () METHOD
2. writelines() METHOD
1. write () METHOD
For sending data in file, i.e. to create /
write in the file, write() and writelines()
methods can be used.
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
1. write () METHOD EXAMPLE
Use ‘a’ in open function to append or
add the information to the file. ‘a+’ to add as
well as read the file.
1. write () METHOD EXAMPLE
if you execute the program n times, the file
is opened in w mode meaning it deletes content
of file and writes fresh every time you run.
1. write () METHOD EXAMPLE O/P
if you execute the program n times, the file
is opened in w mode meaning it deletes content
of file and writes fresh every time you run.
1. write () METHOD EXAMPLE 2
now content of test1.txt is changed
because the file is opened in w mode and this
mode deletes all content and writes fresh if file
exists. n write to next line. If not used it writes
on the same line.
1. write () METHOD EXAMPLE 2
So test1.txt is already exists in harddisk
and it deletes all content and writes fresh. If file
not found it creates new file.
1. write () METHOD EXAMPLE 2
So test1.txt is already exists in harddisk
and it deletes all content and writes fresh. If file
not found it creates new file.
2. writelines() METHOD
For writing a string at a time, we use
write() method, it can't be used for writing a
list, tuple etc. into a file.
Sequence data type can be written using
writelines() method in the file. It's not that, we
can't write a string using writelines() method.
It's syntax is:
fileobject.writelines(seq)
2. writelines() METHOD
So, whenever we have to write a
sequence of string / data type, we will use
writelines(), instead of write().
Example:
f = open('test2.txt','w')
str = 'hello world.n this is my first file
handling program.n I am using python
language"
f.writelines(str)
f.close()
RANDOM ACCESS METHODS
RANDOM ACCESS METHODS
All reading and writing functions
discussed till now, work sequentially in the file.
To access the contents of file randomly –
following methods are use.
seek method
tell method
seek()method can be used to position the
file object at particular place in the file.
It's syntax is :
fileobject.seek(offset [, from_what])
here offset is used to calculate the
position of fileobject in the file in bytes. Offset
is added to from_what (reference point) to get
the position. Following is the list of from_what
values:
seek method
Value reference point
0 beginning of the file
1 current position of file
2 end of file
default value of from_what is 0, i.e. beginning
of the file.
seek method
seek method
f.seek(7) keeps file pointer at reads the file
content from 8th position onwards to till EOF.
seek method
Results: - So 8th position onwards to till EOF.
Reading according to size
In the input function if you specify the
number of bytes that many number of bytes
can be fetched and assigned to an identifier.
Reading according to size
f.read(1) will read single byte/ character
starting from byte number 8. hence byte
number 8 is P so one character/byte is fetched
and assigned to f_data identifier.
Reading according to size
f.read(2) - will read 2 chars/bytes
f.read(4) - will read 4 chars/bytes
and so on..
tell() method returns an integer giving
the current position of object in the file. The
integer returned specifies the number of bytes
from the beginning of the file till the current
position of file object.
It's syntax is
fileobject.tell()
tell method
tell() method
tell() method
tell() method returns an integer and
assigned to pos variable. It is the current
position from the beginning of file.
BINARY FILES
CREATING BINARY FILES
CREATING BINARY FILES
SEEING CONTENT OF BINARY FILE
CONTENT OF BINARY FILE
Content of binary file which is in codes.
READING BINARY FILES TROUGH PROGRAM
READING BINARY FILE PROGRAM
READING BINARY FILE PROGRAM
CONTENT OF BINARY FILE
PICKELING AND UNPICKLING USING
PICKEL MODULE
PICKELING AND UNPICKLING USING
PICKEL MODULE
Use the python module pickle for
structured data such as list or directory to a
file.
PICKLING refers to the process of
converting the structure to a byte stream
before writing to a file.
while reading the contents of the file,
a reverse process called UNPICKLING is used
to convert the byte stream back to the
original structure.
PICKELING AND UNPICKLING USING
PICKEL MODULE
First we need to import the module, It
provides two main methods for the purpose:-
1) dump() method
2) load() method
pickle.dump() Method
Use pickle.dump() method to write the
object in file which is opened in binary access
mode.
Syntax of dump method is:
dump(object,fileobject)
pickle.dump() Method
pickle.dump() Method
# A program to write list sequence in a
binary file
pickle.dump() Method
OUTPUT
pickle.dump() Method
Once you try to open list.dat file in
python editor to see the content python
generates decoding error.
pickle.load() Method
pickle.load() Method
pickle.load() method is used to read the
binary file.
pickle.load() Method
pickle.load() method is used to read the
binary file.
DIFFERENCE BETWEEN TEXT FILES AND
BINARY FILES
DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES
DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES
Text Files Binary Files
1. Text Files are
sequential files
1. A Binary file contain
arbitrary binary data
2. Text files only stores
texts
2 Binary Files are used
to store binary data
such as image, video,
audio, text
3. There is a delimiter
EOL (End of Line i.e n)
3. There is no delimiter
DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES
Text Files Binary Files
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.
5. Text files easy to
understand because
these files are in
human readable form
5. Binary files are
difficult to understand.
DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES
Text Files Binary Files
6. Text files are having
extension .txt
6. Binary files are
having .dat extension
7. Programming on text
files are very easy.
7. Programming on
binary files are difficult.
DIFFERENCE BETWEEN TEXT FILES AND
BINARY FILES
TEXT FILE BINARY FILE
1. Bits represent
character.
1. Bits represent a
custom data.
2. Less prone to get
corrupt as changes
reflect as soon as the
file is opened and can
easily be undone
2. Can easily get
corrupted, even a single
bit change may corrupt
the file.
DIFFERENCE BETWEEN TEXT FILES AND
BINARY FILES
TEXT FILE BINARY FILE
3. Can store only plain
text in a file.
3. Can store different
types of data (image,
audio, text) in a single
file.
4. Widely used file
format and can be
opened using any
simple text editor.
4. Developed especially
for an application and
may not be understood
by other applications.
DIFFERENCE BETWEEN TEXT FILES AND
BINARY FILES
TEXT FILE BINARY FILE
5. Mostly .txt and .rtf
are used as extensions
to text files.
5. Can have any
application defined
extension.
PYTHON FILE OBJECT ATTRIBUTES
PYTHON FILE OBJECT ATTRIBUTES
File attributes give information about the
file and file state.
Attribute Function
name Returns the name of the file
closed
Returns true if file is closed. False
otherwise.
mode The mode in which file is open.
softspace
Returns a Boolean that indicates
whether a space character needs to
be printed before another value
when using the print statement.
OTHER METHODS OF FILEOBJECT
OTHER METHODS OF FILEOBJECT
Method Function
readable()
Returns True/False whether file is
readable
writable()
Returns True/False whether file is
writable
fileno()
Return the Integer descriptor used by
Python to request I/O operations from
Operating System
flush() Clears the internal buffer for the file.
OTHER METHODS OF FILEOBJECT
Method Function
isatty()
Returns True if file is connected to a
Tele-TYpewriter (TTY) device or
something similar.
truncate([size) Truncate the file, up to specified bytes.
next(iterator,[def
ault])
Iterate over a file when file is used as an
iterator, stops iteration when reaches
end-of-file (EOF) for reading.
HANDLING FILES THROUGH
OS MODULE
HANDLING FILES THROUGH
OS MODULE
The os module of Python allows you to
perform Operating System dependent
operations such as making a folder, listing
contents of a folder, know about a process, end
a process etc..
Let's see some useful os module methods
that can help you to handle files and folders in
your program.
ABSOLUTE PATH AND RELATIVE PATH
ABSOLUTE PATH
Absolute path of file is file location, where
in it starts from the top most directory
ABSOLUTE PATH
RELATIVE PATH
Relative Path of file is file location, where
in it starts from the current working directory
Myfoldermyfile.txt
RELATIVE PATH
HANDLING FILES THROUGH
OS MODULE
Method Function
os.makedirs() Create a new folder
os.listdir() List the contents of a folder
os.getcwd() Show current working directory
os.path.getsize()
show file size in bytes of file
passed
in parameter
os.path.isfile() Is passed parameter a file
os.path.isdir() Is passed parameter a folder
os.chdir Change directory/folder
HANDLING FILES THROUGH
OS MODULE
Method Function
os.rename(current,new) Rename a file
os.remove(file_name) Delete a file
PROGRAMS
PROGRAMS
1. Write a function to create a text file containing
following data
Neither apple nor pine are in pineapple. Boxing
rings are square.
Writers write, but fingers don't fing. Overlook and
oversee are opposites. A house can burn up as it
burns down. An alarm goes off by going on.
PROGRAMS
2. Read back the entire file content using read()
or readlines () and display on screen.
3. Append more text of your choice in the file
and display the content of file with line numbers
prefixed to line.
4. Display last line of file.
Contd..
PROGRAMS
5. Display first line from 10th character
onwards
6. Read and display a line from the file. Ask
user to provide the line number to be read.
PROGRAMS
7. A text file named MATTER.TXT contains some
text, which needs to be displayed such that
every next character is separated by a symbol
‘#’.
8. Write a statement in Python to open a text
file STORY.TXT so that new contents can be
added at the end of it.
PROGRAMS
9. Write a method in Python to read lines from a
text file INDIA.TXT, to find and display the
occurrence of the word ‘‘India’’.
For example :
If the content of the file is
‘‘India is the fastest growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is
capable of reaching.’’
The output should be 4.
PROGRAMS
10. Write a function to count total number of spaces,
lines and characters in a given line of text
PROGRAMS
11. Write a function to count total number of digits in
a given line of text
PROGRAMS
12. Write a function to copy the content of notes.txt
to sub.txt
NOTES.txt FILE
PROGRAMS
12. Write a function to copy the content of notes.txt
to sub.txt
PROGRAM - OUTPUT
12. Write a function to copy the content of notes.txt
to sub.txt
SUB.txt FILE
PROGRAM - OUTPUT
13. Write a function to append or add a line of text in
notes.txt
NOTES.txt FILE
PROGRAMS
13. Write a function to append or add a line of text in
notes.txt
PROGRAM -OUTPUT
13. Write a function to append or add a line of text in
notes.txt
PROGRAMS
14. Write a function to display total number of lines
in a notes.txt file.
NOTES.txt FILE
PROGRAMS
14. Write a function to display total number of lines
in a notes.txt file.
NOTES.txt FILE
PROGRAMS
14. Write a function to display total number of lines
in a notes.txt file.
PROGRAM - OUTPUT
14. Write a function to display total number of lines
in a notes.txt file.
OUTPUT
PROGRAMS
14. Write a function in python to count the
total number of lines in a file ‘Mem.txt’
ANTOHER METHOD
PROGRAMS
14. Write a function in python to count the
total number of lines in a file ‘Mem.txt’
‘Mem.txt’ file contains
PROGRAMS
14. Write a function in python to count the
total number of lines in a file ‘Mem.txt’
PROGRAMS
15. Write a function to display last line of notes.txt
file.
NOTES.txt FILE
PROGRAMS
15. Write a function to display last line of notes.txt
file.
PROGRAM - OUTPUT
OUTPUT
15. Write a function to display last line of notes.txt
file.
PROGRAMS
16. Write a function COUNT_DO( ) in Python to
count the presence of a word ‘do’ in a text file
“MEMO.TXT”.
Example :
If the content of the file “MEMO.TXT” is as
follows:
I will do it, if you request me to do it.
It would have been done much earlier.
The function COUNT_DO( ) will display the
following message:
Count of -do- in flie:
PROGRAMS
17. Write a function in Python to count the no.
of “Me” or “My” words present in a text file
“DIARY.TXT”.
If the file “DIARY.TXT” content is as follows :
My first book was Me and My family. It gave me
chance to be known to the world.
The output of the function should be Count of
Me/ My in file :
PROGRAMS
18. Write a function in Python to count and
display the number of lines starting with alphabet
‘A’ present in a text file “LINES.TXT”.
Example: If the file “LINES.TXT” contains the
following lines,
A boy is playing there.
There is a playground.
An aeroplane is in the sky.
Alphabets and numbers are allowed in the
password.
The function should display the output as 3.
PROGRAMS
19. Write a function in Python to read the
content of a text file “DELHI.TXT” and display all
those lines on screen, which are Either starting
with ‘D’ or starting with ‘M’.
DELHI.TXT File
PROGRAMS
PROGRAMS
20. Write a function in Python to count the
number of alphabets present in a text file
“NOTES.TXT”.
NOTES.TXT FILE
PROGRAMS
20. Write a function in Python to count the
number of alphabets present in a text file
“NOTES.TXT”.
NOTES.TXT FILE
CBSE QUESTION PAPER QNO 4
CBSE QUESTION PAPER 2015 Delhi
CBSE QUESTION PAPER 2015 Delhi
4(a) Differentiate between the following : 1
(i) f = open(‘diary.txt’, ‘r’)
(ii) f = open(‘diary.txt’, ‘w’)
4(b) Write a method in python to read the
content from a text file diary.txt line by line
and display the same on screen. 2
CBSE QUESTION PAPER 2016 Delhi
CBSE QUESTION PAPER 2016 Delhi
4. (a) Write a statement in Python to perform
the following operations : 1
 To open a text file “BOOK.TXT” in read mode
 To open a text file “BOOK.TXT” in write
mode
4(b) Write a method in python to read the
content from a text file diary.txt line by line
and display the same on screen. 2
CBSE QUESTION PAPER 2017 Delhi
CBSE QUESTION PAPER 2017 Delhi
4 (a) Differentiate between file modes r+ and
rb+ with respect to Python. 1
4(b) Write a method in Python to read lines
from a text file MYNOTES.TXT, and display
those lines, which are starting with the
alphabet K 2
CBSE QUESTION PAPER 2018 AI
CBSE QUESTION PAPER 2018 AI
4 (a) Write a statement in Python to open a text
file STORY.TXT so that new contents can be
added at the end of it. 1
4 (b) Write a method in Python to read lines from
a text file INDIA.TXT, to find and display the
occurrence of the word ‘‘India’’. 2
For example :
If the content of the file is
‘‘India is the fastest growing economy.
CBSE QUESTION PAPER 2018 AI
India is looking for more investments around the
globe.
The whole world is looking at India as a great
market.
Most of the Indians can foresee the heights that
India is capable of reaching.’’
The output should be 4. 2
ADDITIONAL PROGRAMS
ADDITIONAL PROGRAMS
1. Write a Python program to read an entire text
file.
2. Write a Python program to read first n lines of
a file.
3. Write a Python program to append text to a
file and display the text.
4. Write a Python program to read last n lines of
a file.
5. Write a Python program to read last n lines of
a file.
5. Write a Python program to read a file line by
line and store it into a list.
6. Write a Python program to read a file line by
line store it into a variable.
7. Write a Python program to read a file line by
line store it into an array.
8. Write a python program to find the longest
words.
9. Write a Python program to count the number
of lines in a text file.
10. Write a Python program to count the
frequency of words in a file.
ADDITIONAL PROGRAMS
5. Write a Python program to read a file line by
line and store it into a list.
6. Write a Python program to read a file line by
line store it into a variable.
7. Write a Python program to read a file line by
line store it into an array.
8. Write a python program to find the longest
words.
9. Write a Python program to count the number
of lines in a text file.
10. Write a Python program to count the
frequency of words in a file.
ADDITIONAL PROGRAMS
CLASS TEST
1. Write a program to count total no of
spaces,words,characters,lines in a msg.txt file.
2.What is file?
3.Differentiate between text files and binary files.
4.Write a program to copy the content of
notes.txt from 3rd character to till end of file to
para.txt
5. Write a program to create binary file
Class : XII Time: 40 Min
Topic: FILE HANDLING Max Marks: 40
Each Question carries 4 Marks
Thank You

Más contenido relacionado

La actualidad más candente

Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsRanel Padon
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Edureka!
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary fileSanjayKumarMahto1
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing fileskeeeerty
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methodskeeeerty
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 

La actualidad más candente (20)

Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Python - Lecture 11
Python - Lecture 11Python - Lecture 11
Python - Lecture 11
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Python list
Python listPython list
Python list
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 

Similar a Chapter 08 data file handling

DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptxAmitKaur17
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.ssuser00ad4e
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugevsol7206
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is hereCHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is heresidbhat290907
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methodsKeerty Smile
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdfsulekha24
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxAshwini Raut
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdfbotin17097
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in pythonnitamhaske
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesKeerty Smile
 

Similar a Chapter 08 data file handling (20)

DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is hereCHAPTER 2 - FILE HANDLING-txtfile.pdf is here
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
 
FILE HANDLING.pptx
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptx
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
Python-files
Python-filesPython-files
Python-files
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 

Más de Praveen M Jigajinni

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programmingPraveen M Jigajinni
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with pythonPraveen M Jigajinni
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingPraveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow chartsPraveen M Jigajinni
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingPraveen M Jigajinni
 
11 Unit 1 Problem Solving Techniques
11  Unit 1 Problem Solving Techniques11  Unit 1 Problem Solving Techniques
11 Unit 1 Problem Solving TechniquesPraveen M Jigajinni
 

Más de Praveen M Jigajinni (20)

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computing
 
Chapter 2 operating systems
Chapter 2 operating systemsChapter 2 operating systems
Chapter 2 operating systems
 
Chapter 1 computer fundamentals
Chapter 1 computer  fundamentalsChapter 1 computer  fundamentals
Chapter 1 computer fundamentals
 
Chapter 0 syllabus 2019 20
Chapter 0  syllabus 2019 20Chapter 0  syllabus 2019 20
Chapter 0 syllabus 2019 20
 
11 Unit 1 Problem Solving Techniques
11  Unit 1 Problem Solving Techniques11  Unit 1 Problem Solving Techniques
11 Unit 1 Problem Solving Techniques
 

Último

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Último (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Chapter 08 data file handling

  • 1. CHAPTER - 08 DATA FILE HANDLING
  • 2. Unit I Programming and Computational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
  • 4. LEARNING OUTCOMES After going through the chapter, student will be able to: Understand the importance of data file for permanent storage of data. Understand how standard Input/Output function work. Distinguish between text and binary file Open and close a file ( text and binary) Read and write data in file Write programs that manipulate data file(s
  • 5. INTRODUCTION – DATA FILES 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.
  • 7. Basic operations performed on a data file are: 1. Naming a file 2. Opening a file 3. Reading data from the file 4. Writing data in the file 5. Closing a file BASIC OPERATIONS ON FILE
  • 9. Using these basic operations, we can process file in many ways, such as 1. CREATING A FILE 2. TRAVERSING A FILE FOR DISPLAYING THE DATA ON SCREEN 3. APPENDING DATA IN FILE 4. INSERTING DATA IN FILE Contd.. next FILE PROCESSING
  • 10. INTRODUCTION – DATA FILES 6. CREATE A COPY OF FILE 7. UPDATING DATA IN THE FILE … etc 5. DELETING DATA FROM FILE
  • 12. TYPES OF FILES Python allow us to create and manage two types of file 1. TEXT FILE 2. BINARY FILE
  • 13. What is Text File? A text file is usually considered as sequence of lines. Line is a sequence of characters (ASCII or UNICODE), stored on permanent storage media. The default character coding in python is ASCII each line is terminated by a special character, known as End of Line (EOL). At the lowest level, text file will be collection of bytes. Text files are stored in human readable form and they can also be created using any text editor. 1. TEXT FILE
  • 14. What is Binary File? A binary file contains arbitrary binary data i.e. numbers stored in the file, can be used for numerical operation(s). 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. In the case of binary file it is extremely important that we interpret the correct data type while reading the file. Python provides special module(s) for encoding and decoding of data for binary file. 2. BINARY FILE
  • 16. 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])
  • 17. 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.
  • 18. OPENING AND CLOSING FILES The third parameter (buffering) is for specifying how much is read from the file in one read. Finally, The function will return an object of file type using which we will manipulate the file, in our program. 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.
  • 19. OPENING AND CLOSING FILES 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
  • 21. 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.
  • 22. 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.
  • 23. 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.
  • 24. 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.
  • 25. 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.
  • 26. FILE ACCESS MODES - EXAMPLE For Ex: f=open(“tests.dat ”, ‘rb’) tests.dat is a binary file and is opened in read only mode.
  • 27. 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.
  • 28. 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.
  • 30. 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()
  • 32. FILE READING METHODS PYTHON PROGRAM A Program reads a text/binary file from hard disk. File acts like an input to a program.
  • 33. FILE READING METHODS Followings are the methods to read a data from the file. 1. readline() METHOD 2. readlines() METHOD 3. read() METHOD
  • 34. readline() METHOD readline() will return a line read, as a string from the file. First call to function will return first line, second call next line and so on. It's syntax is, fileobject.readline()
  • 35. readline() EXAMPLE First create a text file and save under filename notes.txt
  • 37. readline() EXAMPLE O/P readline() will return only one line from a file, but notes.txt file containing three lines of text
  • 38. readlines() METHOD readlines()can be used to read the entire content of the file. You need to be careful while using it w.r.t. size of memory required before using the function. The method will return a list of strings, each separated by n. An example of reading entire data of file in list is: It's syntax is, fileobject.readlines() as it returns a list, which can then be used for manipulation.
  • 40. readlines() EXAMPLE The readlines() method will return a list of strings, each separated by n
  • 41. read() METHOD The read() method is used to read entire file The syntax is: fileobject.read() For example Contd…
  • 43. read() METHOD EXAMPLE O/P The read() method will return entire file.
  • 45. read(size) METHOD read() can be used to read specific size string from file. This function also returns a string read from the file. Syntax of read() function is: fileobject.read([size]) For Example: f.read(1) will read single byte or character from a file.
  • 46. read(size) METHOD EXAMPLE f.read(1) will read single byte or character from a file and assigns to x.
  • 49. WRITING METHODS PYTHON PROGRAM A Program writes into a text/binary file from hard disk.
  • 50. WRITING METHODS 1. write () METHOD 2. writelines() METHOD
  • 51. 1. write () METHOD For sending data in file, i.e. to create / write in the file, write() and writelines() methods can be used. 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
  • 52. 1. write () METHOD EXAMPLE Use ‘a’ in open function to append or add the information to the file. ‘a+’ to add as well as read the file.
  • 53. 1. write () METHOD EXAMPLE if you execute the program n times, the file is opened in w mode meaning it deletes content of file and writes fresh every time you run.
  • 54. 1. write () METHOD EXAMPLE O/P if you execute the program n times, the file is opened in w mode meaning it deletes content of file and writes fresh every time you run.
  • 55. 1. write () METHOD EXAMPLE 2 now content of test1.txt is changed because the file is opened in w mode and this mode deletes all content and writes fresh if file exists. n write to next line. If not used it writes on the same line.
  • 56. 1. write () METHOD EXAMPLE 2 So test1.txt is already exists in harddisk and it deletes all content and writes fresh. If file not found it creates new file.
  • 57. 1. write () METHOD EXAMPLE 2 So test1.txt is already exists in harddisk and it deletes all content and writes fresh. If file not found it creates new file.
  • 58. 2. writelines() METHOD For writing a string at a time, we use write() method, it can't be used for writing a list, tuple etc. into a file. Sequence data type can be written using writelines() method in the file. It's not that, we can't write a string using writelines() method. It's syntax is: fileobject.writelines(seq)
  • 59. 2. writelines() METHOD So, whenever we have to write a sequence of string / data type, we will use writelines(), instead of write(). Example: f = open('test2.txt','w') str = 'hello world.n this is my first file handling program.n I am using python language" f.writelines(str) f.close()
  • 61. RANDOM ACCESS METHODS All reading and writing functions discussed till now, work sequentially in the file. To access the contents of file randomly – following methods are use. seek method tell method
  • 62. seek()method can be used to position the file object at particular place in the file. It's syntax is : fileobject.seek(offset [, from_what]) here offset is used to calculate the position of fileobject in the file in bytes. Offset is added to from_what (reference point) to get the position. Following is the list of from_what values: seek method
  • 63. Value reference point 0 beginning of the file 1 current position of file 2 end of file default value of from_what is 0, i.e. beginning of the file. seek method
  • 64. seek method f.seek(7) keeps file pointer at reads the file content from 8th position onwards to till EOF.
  • 65. seek method Results: - So 8th position onwards to till EOF.
  • 66. Reading according to size In the input function if you specify the number of bytes that many number of bytes can be fetched and assigned to an identifier.
  • 67. Reading according to size f.read(1) will read single byte/ character starting from byte number 8. hence byte number 8 is P so one character/byte is fetched and assigned to f_data identifier.
  • 68. Reading according to size f.read(2) - will read 2 chars/bytes f.read(4) - will read 4 chars/bytes and so on..
  • 69. tell() method returns an integer giving the current position of object in the file. The integer returned specifies the number of bytes from the beginning of the file till the current position of file object. It's syntax is fileobject.tell() tell method
  • 71. tell() method tell() method returns an integer and assigned to pos variable. It is the current position from the beginning of file.
  • 75. SEEING CONTENT OF BINARY FILE
  • 76. CONTENT OF BINARY FILE Content of binary file which is in codes.
  • 77. READING BINARY FILES TROUGH PROGRAM
  • 81. PICKELING AND UNPICKLING USING PICKEL MODULE
  • 82. PICKELING AND UNPICKLING USING PICKEL MODULE Use the python module pickle for structured data such as list or directory to a file. PICKLING refers to the process of converting the structure to a byte stream before writing to a file. while reading the contents of the file, a reverse process called UNPICKLING is used to convert the byte stream back to the original structure.
  • 83. PICKELING AND UNPICKLING USING PICKEL MODULE First we need to import the module, It provides two main methods for the purpose:- 1) dump() method 2) load() method
  • 84. pickle.dump() Method Use pickle.dump() method to write the object in file which is opened in binary access mode. Syntax of dump method is: dump(object,fileobject)
  • 86. pickle.dump() Method # A program to write list sequence in a binary file
  • 88. pickle.dump() Method Once you try to open list.dat file in python editor to see the content python generates decoding error.
  • 90. pickle.load() Method pickle.load() method is used to read the binary file.
  • 91. pickle.load() Method pickle.load() method is used to read the binary file.
  • 92. DIFFERENCE BETWEEN TEXT FILES AND BINARY FILES
  • 93. DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES
  • 94. DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES Text Files Binary Files 1. Text Files are sequential files 1. A Binary file contain arbitrary binary data 2. Text files only stores texts 2 Binary Files are used to store binary data such as image, video, audio, text 3. There is a delimiter EOL (End of Line i.e n) 3. There is no delimiter
  • 95. DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES Text Files Binary Files 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. 5. Text files easy to understand because these files are in human readable form 5. Binary files are difficult to understand.
  • 96. DIFFERENCE BETWEEN TEXT FILESAND BINARY FILES Text Files Binary Files 6. Text files are having extension .txt 6. Binary files are having .dat extension 7. Programming on text files are very easy. 7. Programming on binary files are difficult.
  • 97. DIFFERENCE BETWEEN TEXT FILES AND BINARY FILES TEXT FILE BINARY FILE 1. Bits represent character. 1. Bits represent a custom data. 2. Less prone to get corrupt as changes reflect as soon as the file is opened and can easily be undone 2. Can easily get corrupted, even a single bit change may corrupt the file.
  • 98. DIFFERENCE BETWEEN TEXT FILES AND BINARY FILES TEXT FILE BINARY FILE 3. Can store only plain text in a file. 3. Can store different types of data (image, audio, text) in a single file. 4. Widely used file format and can be opened using any simple text editor. 4. Developed especially for an application and may not be understood by other applications.
  • 99. DIFFERENCE BETWEEN TEXT FILES AND BINARY FILES TEXT FILE BINARY FILE 5. Mostly .txt and .rtf are used as extensions to text files. 5. Can have any application defined extension.
  • 100. PYTHON FILE OBJECT ATTRIBUTES
  • 101. PYTHON FILE OBJECT ATTRIBUTES File attributes give information about the file and file state. Attribute Function name Returns the name of the file closed Returns true if file is closed. False otherwise. mode The mode in which file is open. softspace Returns a Boolean that indicates whether a space character needs to be printed before another value when using the print statement.
  • 102. OTHER METHODS OF FILEOBJECT
  • 103. OTHER METHODS OF FILEOBJECT Method Function readable() Returns True/False whether file is readable writable() Returns True/False whether file is writable fileno() Return the Integer descriptor used by Python to request I/O operations from Operating System flush() Clears the internal buffer for the file.
  • 104. OTHER METHODS OF FILEOBJECT Method Function isatty() Returns True if file is connected to a Tele-TYpewriter (TTY) device or something similar. truncate([size) Truncate the file, up to specified bytes. next(iterator,[def ault]) Iterate over a file when file is used as an iterator, stops iteration when reaches end-of-file (EOF) for reading.
  • 106. HANDLING FILES THROUGH OS MODULE The os module of Python allows you to perform Operating System dependent operations such as making a folder, listing contents of a folder, know about a process, end a process etc.. Let's see some useful os module methods that can help you to handle files and folders in your program.
  • 107. ABSOLUTE PATH AND RELATIVE PATH
  • 108. ABSOLUTE PATH Absolute path of file is file location, where in it starts from the top most directory ABSOLUTE PATH
  • 109. RELATIVE PATH Relative Path of file is file location, where in it starts from the current working directory Myfoldermyfile.txt RELATIVE PATH
  • 110. HANDLING FILES THROUGH OS MODULE Method Function os.makedirs() Create a new folder os.listdir() List the contents of a folder os.getcwd() Show current working directory os.path.getsize() show file size in bytes of file passed in parameter os.path.isfile() Is passed parameter a file os.path.isdir() Is passed parameter a folder os.chdir Change directory/folder
  • 111. HANDLING FILES THROUGH OS MODULE Method Function os.rename(current,new) Rename a file os.remove(file_name) Delete a file
  • 113. PROGRAMS 1. Write a function to create a text file containing following data Neither apple nor pine are in pineapple. Boxing rings are square. Writers write, but fingers don't fing. Overlook and oversee are opposites. A house can burn up as it burns down. An alarm goes off by going on.
  • 114. PROGRAMS 2. Read back the entire file content using read() or readlines () and display on screen. 3. Append more text of your choice in the file and display the content of file with line numbers prefixed to line. 4. Display last line of file. Contd..
  • 115. PROGRAMS 5. Display first line from 10th character onwards 6. Read and display a line from the file. Ask user to provide the line number to be read.
  • 116. PROGRAMS 7. A text file named MATTER.TXT contains some text, which needs to be displayed such that every next character is separated by a symbol ‘#’. 8. Write a statement in Python to open a text file STORY.TXT so that new contents can be added at the end of it.
  • 117. PROGRAMS 9. Write a method in Python to read lines from a text file INDIA.TXT, to find and display the occurrence of the word ‘‘India’’. For example : If the content of the file is ‘‘India is the fastest growing economy. India is looking for more investments around the globe. The whole world is looking at India as a great market. Most of the Indians can foresee the heights that India is capable of reaching.’’ The output should be 4.
  • 118. PROGRAMS 10. Write a function to count total number of spaces, lines and characters in a given line of text
  • 119. PROGRAMS 11. Write a function to count total number of digits in a given line of text
  • 120. PROGRAMS 12. Write a function to copy the content of notes.txt to sub.txt NOTES.txt FILE
  • 121. PROGRAMS 12. Write a function to copy the content of notes.txt to sub.txt
  • 122. PROGRAM - OUTPUT 12. Write a function to copy the content of notes.txt to sub.txt SUB.txt FILE
  • 123. PROGRAM - OUTPUT 13. Write a function to append or add a line of text in notes.txt NOTES.txt FILE
  • 124. PROGRAMS 13. Write a function to append or add a line of text in notes.txt
  • 125. PROGRAM -OUTPUT 13. Write a function to append or add a line of text in notes.txt
  • 126. PROGRAMS 14. Write a function to display total number of lines in a notes.txt file. NOTES.txt FILE
  • 127. PROGRAMS 14. Write a function to display total number of lines in a notes.txt file. NOTES.txt FILE
  • 128. PROGRAMS 14. Write a function to display total number of lines in a notes.txt file.
  • 129. PROGRAM - OUTPUT 14. Write a function to display total number of lines in a notes.txt file. OUTPUT
  • 130. PROGRAMS 14. Write a function in python to count the total number of lines in a file ‘Mem.txt’ ANTOHER METHOD
  • 131. PROGRAMS 14. Write a function in python to count the total number of lines in a file ‘Mem.txt’ ‘Mem.txt’ file contains
  • 132. PROGRAMS 14. Write a function in python to count the total number of lines in a file ‘Mem.txt’
  • 133. PROGRAMS 15. Write a function to display last line of notes.txt file. NOTES.txt FILE
  • 134. PROGRAMS 15. Write a function to display last line of notes.txt file.
  • 135. PROGRAM - OUTPUT OUTPUT 15. Write a function to display last line of notes.txt file.
  • 136. PROGRAMS 16. Write a function COUNT_DO( ) in Python to count the presence of a word ‘do’ in a text file “MEMO.TXT”. Example : If the content of the file “MEMO.TXT” is as follows: I will do it, if you request me to do it. It would have been done much earlier. The function COUNT_DO( ) will display the following message: Count of -do- in flie:
  • 137. PROGRAMS 17. Write a function in Python to count the no. of “Me” or “My” words present in a text file “DIARY.TXT”. If the file “DIARY.TXT” content is as follows : My first book was Me and My family. It gave me chance to be known to the world. The output of the function should be Count of Me/ My in file :
  • 138. PROGRAMS 18. Write a function in Python to count and display the number of lines starting with alphabet ‘A’ present in a text file “LINES.TXT”. Example: If the file “LINES.TXT” contains the following lines, A boy is playing there. There is a playground. An aeroplane is in the sky. Alphabets and numbers are allowed in the password. The function should display the output as 3.
  • 139. PROGRAMS 19. Write a function in Python to read the content of a text file “DELHI.TXT” and display all those lines on screen, which are Either starting with ‘D’ or starting with ‘M’. DELHI.TXT File
  • 141. PROGRAMS 20. Write a function in Python to count the number of alphabets present in a text file “NOTES.TXT”. NOTES.TXT FILE
  • 142. PROGRAMS 20. Write a function in Python to count the number of alphabets present in a text file “NOTES.TXT”. NOTES.TXT FILE
  • 144. CBSE QUESTION PAPER 2015 Delhi
  • 145. CBSE QUESTION PAPER 2015 Delhi 4(a) Differentiate between the following : 1 (i) f = open(‘diary.txt’, ‘r’) (ii) f = open(‘diary.txt’, ‘w’) 4(b) Write a method in python to read the content from a text file diary.txt line by line and display the same on screen. 2
  • 146. CBSE QUESTION PAPER 2016 Delhi
  • 147. CBSE QUESTION PAPER 2016 Delhi 4. (a) Write a statement in Python to perform the following operations : 1  To open a text file “BOOK.TXT” in read mode  To open a text file “BOOK.TXT” in write mode 4(b) Write a method in python to read the content from a text file diary.txt line by line and display the same on screen. 2
  • 148. CBSE QUESTION PAPER 2017 Delhi
  • 149. CBSE QUESTION PAPER 2017 Delhi 4 (a) Differentiate between file modes r+ and rb+ with respect to Python. 1 4(b) Write a method in Python to read lines from a text file MYNOTES.TXT, and display those lines, which are starting with the alphabet K 2
  • 151. CBSE QUESTION PAPER 2018 AI 4 (a) Write a statement in Python to open a text file STORY.TXT so that new contents can be added at the end of it. 1 4 (b) Write a method in Python to read lines from a text file INDIA.TXT, to find and display the occurrence of the word ‘‘India’’. 2 For example : If the content of the file is ‘‘India is the fastest growing economy.
  • 152. CBSE QUESTION PAPER 2018 AI India is looking for more investments around the globe. The whole world is looking at India as a great market. Most of the Indians can foresee the heights that India is capable of reaching.’’ The output should be 4. 2
  • 154. ADDITIONAL PROGRAMS 1. Write a Python program to read an entire text file. 2. Write a Python program to read first n lines of a file. 3. Write a Python program to append text to a file and display the text. 4. Write a Python program to read last n lines of a file. 5. Write a Python program to read last n lines of a file.
  • 155. 5. Write a Python program to read a file line by line and store it into a list. 6. Write a Python program to read a file line by line store it into a variable. 7. Write a Python program to read a file line by line store it into an array. 8. Write a python program to find the longest words. 9. Write a Python program to count the number of lines in a text file. 10. Write a Python program to count the frequency of words in a file. ADDITIONAL PROGRAMS
  • 156. 5. Write a Python program to read a file line by line and store it into a list. 6. Write a Python program to read a file line by line store it into a variable. 7. Write a Python program to read a file line by line store it into an array. 8. Write a python program to find the longest words. 9. Write a Python program to count the number of lines in a text file. 10. Write a Python program to count the frequency of words in a file. ADDITIONAL PROGRAMS
  • 157. CLASS TEST 1. Write a program to count total no of spaces,words,characters,lines in a msg.txt file. 2.What is file? 3.Differentiate between text files and binary files. 4.Write a program to copy the content of notes.txt from 3rd character to till end of file to para.txt 5. Write a program to create binary file Class : XII Time: 40 Min Topic: FILE HANDLING Max Marks: 40 Each Question carries 4 Marks