SlideShare a Scribd company logo
2
Most read
7
Most read
19
Most read
Data File Handling
Based on CBSE Curriculum
Class-12
By:
Neha Tyagi, PGT CS
KV No-5, 2nd Shift, Jaipur,
Jaipur Region
Neha Tyagi, KV No-5, Jaipur
Data File Handling
• We have seen yet only the transient programs. The programs which run
for a short period of time and give some output and after that their data is
disappeared. And when we again run those programs then we have to use
new data.
• This is because the data is entered in primary memory which is temporary
memory and its data is volatile.
• Those programs which are persistent i.e. they are always in running or run
for a long time then their data is stored in permanent storage (e.g.
harddisk) . If the program is closed or restarted then the data used will be
retrieved.
• For this purpose the program should have the capability to read or write
the text files or data files. These files can be saved in permanent storage.
• The meaning of File I/O (input-output) is to transfer the data from Primary
memory to secondary memory and vice-versa.
Neha Tyagi, KV No-5, Jaipur
Hard
Disk
Program in RAM
(Random Access
Memory)
User
Why the Files are used?
• The data stored with in a file is known as persistent data because
this data is permanently stored in the system.
• Python provides reading and writing capability of data files.
• We save the data in the files for further use.
• As you save your data in files using word, excel etc. same thing we
can do with python.
• “A File is a collection of characters in which we can perform read
and write functions. And also we can save it in secondary storage.”
Neha Tyagi, KV No-5, Jaipur
Python
Program
External
File
(Secondar
y Storage)
Write to file
(Save)
Read from
file (Load)
Data File Operations
Following main operations can be done on files -
1. Opening a file
2. Performing operations
1. READ
2. WRITE etc.
3. Closing The File
Beside above operations there are some more operations can be done on
files.-
• Creating of Files
• Traversing of Data
• Appending Data into file.
• Inserting Data into File.
• Deleting Data from File.
• Copying of File.
• Updating Data into File.
Neha Tyagi, KV No-5, Jaipur
Open
File
Process
Data
Close
File
File Types
File are of two types –
1. Text File: A text file is sequence of line and line is the
sequence of characters and this file is saved in a permanent
storage device. Although in python default character coding
is ASCII but by using constant ‘U’ this can be converted into
UNICODE. In Text File each line terminates with a special
character which is EOL (End Of Line). These are in human
readable form and these can be created using any text
editor.
2. Binary File: Binary files are used to store binary data such
as images, videos audio etc. Generally numbers are stored
in binary files. In binary file, there is no delimiter to end a line.
Since they are directly in the form of binary hence there is no
need to translate them. That’s why these files are easy and
fast in working.
Neha Tyagi, KV No-5, Jaipur
Opening & Closing Files
• We need a file variable or file handle to work with files in Python.
• This file object can be created by using open( ) function or file( )
function.
• Open( ) function creates a file object, which is used later to access
the file using the functions related to file manipulation.
• Its syntax is following -
<file_object>=open(<file_name>,<access_mode>)
• File accessing modes -
– read(r): To read the file
– write(w): to write to the file
– append(a): to Write at the end of file.
Neha Tyagi, KV No-5, Jaipur
Python
Program
External
File
(Seconda
ry
Storage)
Read from
file (Load)
Opening & Closing Files. . .
Neha Tyagi, KV No-5, Jaipur
Here the point is that the file “Hello.txt” which is used here is pre
built and stored in the same folder where Python is installed.
Opened the File
The file is closed.
Output
A program describing the functions of file handling.
File Modes
Neha Tyagi, KV No-5, Jaipur
Mode Description
r To read the file which is already existing.
rb Read Only in binary format.
r+ To Read and write but the file pointer will be at the beginning of the file.
rb+ To Read and write binary file. But the file pointer will be at the beginning of
the file.
w Only writing mode, if file is existing the old file will be overwritten else the new
file will be created.
wb Binary file only in writing mode, if file is existing the old file will be overwritten
else the new file will be created.
wb+ Binary file only in reading and writing mode, if file is existing the old file will be
overwritten else the new file will be created.
a Append mode. The file pointer will be at the end of the file.
ab Append mode in binary file. The file pointer will be at the end of the file.
a+ Appending and reading if the file is existing then file pointer will be at the end
of the file else new file will be created for reading and writing.
ab+ Appending and reading in binary file if the file is existing then file pointer will
be at the end of the file else new file will be created for reading and writing.
Reading a File
Neha Tyagi, KV No-5, Jaipur
Output
A Program to read
“Hello.txt” File.
Hello.txt file was
created using
notepad.|
Reading a File . . .
Neha Tyagi, KV No-5, Jaipur
Output
Reading first 10
characters from the
file “Hello.txt”
1. We can also use readline( ) function which can read one line at a
time from the file.
2. Same readlines( ) function is used to read many lines.
Writing to a File
Neha Tyagi, KV No-5, Jaipur
Output
A program to write in
“Hello.txt”
This “Hello.txt” is created using above
program.
• We can write characters into file by using following two methods -
1. write (string)
2. writelines (sequence of lines)
• write( ) : it takes a sting as argument and adds to the file. We have to use
‘n’ in string for end of line character .
• writelines ( ) : if we want to write list, tupleinto the file then we use
writelines ( ) function.
Writing to a File. . .
Neha Tyagi, KV No-5, Jaipur
Output
A Program to use writelines()
function
“Hello.txt” File is
created using the
above program.
Writing to a File.
Neha Tyagi, KV No-5, Jaipur
Output
Hello.txt file is opened using “with”.
“Hello.txt” File is
created using the
above program.
Appending in a File
Neha Tyagi, KV No-5, Jaipur
Output
A program to append
into a file “Hello.Txt”
A new data is appended into
Hello.txt by above program.
• Append means adding something new to existing file.
• ‘a’ mode is used to accomplish this task. It means opening a file in
write mode and if file is existing then adding data to the end of the
file.
Writing User Input to the File.
Neha Tyagi, KV No-5, Jaipur
Output
Taking the data from
user and writing this
data to the file
“Student.txt”.
Student File is
created by using
the above
program.
Operations in Binary File.
Neha Tyagi, KV No-5, Jaipur
• If we want to write structure such as list, dictionary etc and also we
want to read it then we have to use a module in python known as
pickle.
• Pickling means converting structure into byte stream before writing
the data into file.
• And when we read a file then a opposite operation is to be done
means unpickling.
• Pickle module has two methods - dump( ) to write and load( ) to read.
Operations in Binary File
• To read Binary file use of load ( ) function -
Relative and Absolute Paths
Neha Tyagi, KV No-5, Jaipur
• We all know that the files are kept in directory which are also
known as folders.
• Every running program has a current directory. Which is
generally a default directory and python always see the default
directory first.
• OS module provides many such functions which can be used to
work with files and directories. OS means Operating System.
• getcwd( ) is a very function which can be used to identify the
current working directory.
Standard File Streams
Neha Tyagi, KV No-5, Jaipur
• We use standard I/O Streams to get better performance from
different I/O devices.
• Some Standard Streams in python are as follows -
– Standard input Stream sys.stdin
– Standard output Stream sys.stdout
– Standard error Stream sys.stderr
Thank you
Follow our blog to get more chapters and videos
Neha Tyagi, KV No-5, Jaipur
www.pythontrends.wordpress.com

More Related Content

PDF
loops in C ppt.pdf
PPTX
Inline Functions and Default arguments
PPTX
Asymptotic Notations
PDF
01. design & analysis of agorithm intro & complexity analysis
PPTX
Polymorphism Using C++
PPTX
Abstract algebra - Algebraic closed field, unit - 2 , M.Sc. l semester Maths
PPT
C++ Interview Questions
PPTX
Templates in c++
loops in C ppt.pdf
Inline Functions and Default arguments
Asymptotic Notations
01. design & analysis of agorithm intro & complexity analysis
Polymorphism Using C++
Abstract algebra - Algebraic closed field, unit - 2 , M.Sc. l semester Maths
C++ Interview Questions
Templates in c++

What's hot (20)

PPTX
Asymptotic notations
PPTX
Expression and Operartor In C Programming
PDF
Summation Series
PDF
Module 05 Preprocessor and Macros in C
PPT
Link List
PPTX
Heaviside's function
PPTX
Type casting in java
PPT
Lecture 5 - Structured Programming Language
PPT
08 c++ Operator Overloading.ppt
PPTX
Function in c program
PPT
Data Structures- Part5 recursion
PPTX
Data structure - Graph
PPTX
Python dictionary
PPTX
Virtual function in C++ Pure Virtual Function
PPTX
Destructors
PPT
pre processor directives in C
DOCX
Object Oriented Programming All Unit Notes
PPTX
Asymptotic notations
Expression and Operartor In C Programming
Summation Series
Module 05 Preprocessor and Macros in C
Link List
Heaviside's function
Type casting in java
Lecture 5 - Structured Programming Language
08 c++ Operator Overloading.ppt
Function in c program
Data Structures- Part5 recursion
Data structure - Graph
Python dictionary
Virtual function in C++ Pure Virtual Function
Destructors
pre processor directives in C
Object Oriented Programming All Unit Notes
Ad

Similar to chapter-4-data-file-handlingeng.pdf (20)

PPTX
Chapter - 5.pptx
PPTX
Chapter 08 data file handling
PPTX
DFH PDF-converted.pptx
PPTX
Data File Handling in Python Programming
PPTX
file handling.pptx avlothaan pa thambi popa
PPTX
FILE HANDLING IN PYTHON Presentation Computer Science
PDF
File handling with python class 12th .pdf
PPTX
FILE HANDLING in python to understand basic operations.
PPTX
File Handling in Python -binary files.pptx
PPTX
5-filehandling-2004054567151830 (1).pptx
PDF
Python file handling
PDF
file handling.pdf
PDF
File handling and Dictionaries in python
PPTX
Unit V.pptx
PPTX
FILE HANDLING.pptx
PPTX
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
PPTX
01 file handling for class use class pptx
PPTX
File handling for reference class 12.pptx
PPTX
What is FIle and explanation of text files.pptx
PPTX
file handling in python using exception statement
Chapter - 5.pptx
Chapter 08 data file handling
DFH PDF-converted.pptx
Data File Handling in Python Programming
file handling.pptx avlothaan pa thambi popa
FILE HANDLING IN PYTHON Presentation Computer Science
File handling with python class 12th .pdf
FILE HANDLING in python to understand basic operations.
File Handling in Python -binary files.pptx
5-filehandling-2004054567151830 (1).pptx
Python file handling
file handling.pdf
File handling and Dictionaries in python
Unit V.pptx
FILE HANDLING.pptx
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
01 file handling for class use class pptx
File handling for reference class 12.pptx
What is FIle and explanation of text files.pptx
file handling in python using exception statement
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Final Presentation General Medicine 03-08-2024.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
human mycosis Human fungal infections are called human mycosis..pptx
Computing-Curriculum for Schools in Ghana
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
TR - Agricultural Crops Production NC III.pdf
VCE English Exam - Section C Student Revision Booklet
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

chapter-4-data-file-handlingeng.pdf

  • 1. Data File Handling Based on CBSE Curriculum Class-12 By: Neha Tyagi, PGT CS KV No-5, 2nd Shift, Jaipur, Jaipur Region Neha Tyagi, KV No-5, Jaipur
  • 2. Data File Handling • We have seen yet only the transient programs. The programs which run for a short period of time and give some output and after that their data is disappeared. And when we again run those programs then we have to use new data. • This is because the data is entered in primary memory which is temporary memory and its data is volatile. • Those programs which are persistent i.e. they are always in running or run for a long time then their data is stored in permanent storage (e.g. harddisk) . If the program is closed or restarted then the data used will be retrieved. • For this purpose the program should have the capability to read or write the text files or data files. These files can be saved in permanent storage. • The meaning of File I/O (input-output) is to transfer the data from Primary memory to secondary memory and vice-versa. Neha Tyagi, KV No-5, Jaipur Hard Disk Program in RAM (Random Access Memory) User
  • 3. Why the Files are used? • The data stored with in a file is known as persistent data because this data is permanently stored in the system. • Python provides reading and writing capability of data files. • We save the data in the files for further use. • As you save your data in files using word, excel etc. same thing we can do with python. • “A File is a collection of characters in which we can perform read and write functions. And also we can save it in secondary storage.” Neha Tyagi, KV No-5, Jaipur Python Program External File (Secondar y Storage) Write to file (Save) Read from file (Load)
  • 4. Data File Operations Following main operations can be done on files - 1. Opening a file 2. Performing operations 1. READ 2. WRITE etc. 3. Closing The File Beside above operations there are some more operations can be done on files.- • Creating of Files • Traversing of Data • Appending Data into file. • Inserting Data into File. • Deleting Data from File. • Copying of File. • Updating Data into File. Neha Tyagi, KV No-5, Jaipur Open File Process Data Close File
  • 5. File Types File are of two types – 1. Text File: A text file is sequence of line and line is the sequence of characters and this file is saved in a permanent storage device. Although in python default character coding is ASCII but by using constant ‘U’ this can be converted into UNICODE. In Text File each line terminates with a special character which is EOL (End Of Line). These are in human readable form and these can be created using any text editor. 2. Binary File: Binary files are used to store binary data such as images, videos audio etc. Generally numbers are stored in binary files. In binary file, there is no delimiter to end a line. Since they are directly in the form of binary hence there is no need to translate them. That’s why these files are easy and fast in working. Neha Tyagi, KV No-5, Jaipur
  • 6. Opening & Closing Files • We need a file variable or file handle to work with files in Python. • This file object can be created by using open( ) function or file( ) function. • Open( ) function creates a file object, which is used later to access the file using the functions related to file manipulation. • Its syntax is following - <file_object>=open(<file_name>,<access_mode>) • File accessing modes - – read(r): To read the file – write(w): to write to the file – append(a): to Write at the end of file. Neha Tyagi, KV No-5, Jaipur Python Program External File (Seconda ry Storage) Read from file (Load)
  • 7. Opening & Closing Files. . . Neha Tyagi, KV No-5, Jaipur Here the point is that the file “Hello.txt” which is used here is pre built and stored in the same folder where Python is installed. Opened the File The file is closed. Output A program describing the functions of file handling.
  • 8. File Modes Neha Tyagi, KV No-5, Jaipur Mode Description r To read the file which is already existing. rb Read Only in binary format. r+ To Read and write but the file pointer will be at the beginning of the file. rb+ To Read and write binary file. But the file pointer will be at the beginning of the file. w Only writing mode, if file is existing the old file will be overwritten else the new file will be created. wb Binary file only in writing mode, if file is existing the old file will be overwritten else the new file will be created. wb+ Binary file only in reading and writing mode, if file is existing the old file will be overwritten else the new file will be created. a Append mode. The file pointer will be at the end of the file. ab Append mode in binary file. The file pointer will be at the end of the file. a+ Appending and reading if the file is existing then file pointer will be at the end of the file else new file will be created for reading and writing. ab+ Appending and reading in binary file if the file is existing then file pointer will be at the end of the file else new file will be created for reading and writing.
  • 9. Reading a File Neha Tyagi, KV No-5, Jaipur Output A Program to read “Hello.txt” File. Hello.txt file was created using notepad.|
  • 10. Reading a File . . . Neha Tyagi, KV No-5, Jaipur Output Reading first 10 characters from the file “Hello.txt” 1. We can also use readline( ) function which can read one line at a time from the file. 2. Same readlines( ) function is used to read many lines.
  • 11. Writing to a File Neha Tyagi, KV No-5, Jaipur Output A program to write in “Hello.txt” This “Hello.txt” is created using above program. • We can write characters into file by using following two methods - 1. write (string) 2. writelines (sequence of lines) • write( ) : it takes a sting as argument and adds to the file. We have to use ‘n’ in string for end of line character . • writelines ( ) : if we want to write list, tupleinto the file then we use writelines ( ) function.
  • 12. Writing to a File. . . Neha Tyagi, KV No-5, Jaipur Output A Program to use writelines() function “Hello.txt” File is created using the above program.
  • 13. Writing to a File. Neha Tyagi, KV No-5, Jaipur Output Hello.txt file is opened using “with”. “Hello.txt” File is created using the above program.
  • 14. Appending in a File Neha Tyagi, KV No-5, Jaipur Output A program to append into a file “Hello.Txt” A new data is appended into Hello.txt by above program. • Append means adding something new to existing file. • ‘a’ mode is used to accomplish this task. It means opening a file in write mode and if file is existing then adding data to the end of the file.
  • 15. Writing User Input to the File. Neha Tyagi, KV No-5, Jaipur Output Taking the data from user and writing this data to the file “Student.txt”. Student File is created by using the above program.
  • 16. Operations in Binary File. Neha Tyagi, KV No-5, Jaipur • If we want to write structure such as list, dictionary etc and also we want to read it then we have to use a module in python known as pickle. • Pickling means converting structure into byte stream before writing the data into file. • And when we read a file then a opposite operation is to be done means unpickling. • Pickle module has two methods - dump( ) to write and load( ) to read.
  • 17. Operations in Binary File • To read Binary file use of load ( ) function -
  • 18. Relative and Absolute Paths Neha Tyagi, KV No-5, Jaipur • We all know that the files are kept in directory which are also known as folders. • Every running program has a current directory. Which is generally a default directory and python always see the default directory first. • OS module provides many such functions which can be used to work with files and directories. OS means Operating System. • getcwd( ) is a very function which can be used to identify the current working directory.
  • 19. Standard File Streams Neha Tyagi, KV No-5, Jaipur • We use standard I/O Streams to get better performance from different I/O devices. • Some Standard Streams in python are as follows - – Standard input Stream sys.stdin – Standard output Stream sys.stdout – Standard error Stream sys.stderr
  • 20. Thank you Follow our blog to get more chapters and videos Neha Tyagi, KV No-5, Jaipur www.pythontrends.wordpress.com