SlideShare a Scribd company logo
1 of 20
CBSE
CLASS 12
Ch-5 File Handling
SasikalaJayaprakash
INTRODUTION
 A file in itself is bunch of byes stored on some
storage device like hard –drive etc .
 Files help in storing information permanently .
TEXT FILES
• A text file store information in ASC11 or unicode characters
• line of text is terminated,(delimited )with a special character
known EOL (end of line)
• Default mode
• Extn of text file is .txt
• Translation occur
Access Modes
Opening position of file pointer
File modes Opening position of file pointer
r , rb , r+ , rb+ , r+b Beginning of file
W, wb, w+, wb+, w+b Beginning of file
a+, ab , a+ , ab+, a+b End of file if file exist otherwise
beginning of file
Opening files
<file _object name> =open (<file name >)
<file_ object name> =open(<file name >,<mode>)
with open <file_name > as <file_object
with open (‘a.txt’ , ‘r’ ) as myfile
myfile = open (‘a.txt’, ‘ r’ )
READING FILES
METHOD SYNTAX DESCRIPTION
Read() <filehandle>. read([n]) Read at most n bytes; if no n is
specified , reads the entire file ,
return form of string
Readline() <filehandle>.readline([n]) Reads a line of input , if n is
specified reads at most n bytes,
returns in form of string
Readlines() <filehandle> . readlines() Read all lines and return in a list
Writing onto files
Method Syntax Description
write () <file handle>.write(str1) writes string str1 to file
referenced by <file
handle>
writelines() <file
handle>.writeline(L)
write all strings in list L as
lines to file referenced by
<filehandle>
Standard Input ,Output and error stream
• Standard input device( stdin ) – reads from the
keyboard
• Standard output device (stdout) – prints to the
display and can redirected as standard input
• Standard error device(stderr) – same as stdout but
normally only for errors
Binary files
 A binary file is just a file a that contain information in the
same format in which the information is held in memory
 There is no delimiter for a line
 No translations occur required in binary files
 Faster and easier for a program to read and write
 Extn is .dat file
Pickling and unpickling
• pickling refers to the process of converting the
structure(such as list or dict ) to a byte stream before
writing to the file .
Structure(list or
dictionary)
Pickling Byte stream
pickle.dump ()
Used to write object into file
syntax : pickle.dump(<structure>,fileobject)
file object is the file handle of the file where you write
the contents
pickle.load()
• used to read data from file
• syntax : structure=pickle.load(fileobject)
Load()
import pickle
def read():
f1=open('s2.dat','rb')
try:
while True:
rec=pickle.load(f1)
print(rec)
except EOFError:
f1.close()
read()
Dump()
import pickle
def write():
f1=open('s2.dat','wb')
rec=[ ]
while True
mark1,mark2,mark3=0,0,0
r=int(input("enter rollno:"))
n=input("enter name:")
m1=int(input("enter mark1:"))
m2=int(input("enter mark2:"))
m3=int(input("enter mark3:"))
a=(m1+m2+m3)/3
lst=[r,n,m1,m2,m3,a]
rec.append(lst)
pickle.dump(rec,f1)
f1.close()
write()
CSV Files
• CSV files are comma seperated values , each line
of the file is data record .Each record consist of
one or more fields seperated by commas .Data
stores in the form of rows and columns
• It is a text file
• It is mainly useful in importing and exporting
excel files into csv files and processing in python
• File object , csv object to be created
• Accessing is fast
• Can store large amount of data
• Extn is .csv
Reader () - read from the file
Syntax
csv.reader (file , delimiter)
Default delimiter comma(,)
Delimiter can be of colon( : ),semicolon (:),tab (t), pipe (|))
Writer() – write into the file
syntax
csv.writer(file)
Writer()
import csv
f1=open('new1.csv','w',newline='')
s_writer=csv.writer(f1)
s_writer.writerow(["Name","Location
"])
rec=[ ]
while True:
name=input("enter name:")
Location=input("enter location:")
lst=[name,Location]
rec.append(lst)
ch=input("do u want to enter more
records y/n: ")
if ch=='n':
break
s_writer.writerows(rec)
f1.close()
import csv
def read():
f1=open('new1.csv','r',newline='')
s_reader=csv.reader(f1)
for i in s_reader:
print(i)
f1.close()
read()
Reader ()
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file

More Related Content

What's hot

What's hot (20)

String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
interface with mysql.pptx
interface with mysql.pptxinterface with mysql.pptx
interface with mysql.pptx
 
11 Unit 1 Chapter 02 Python Fundamentals
11  Unit 1 Chapter 02 Python Fundamentals11  Unit 1 Chapter 02 Python Fundamentals
11 Unit 1 Chapter 02 Python Fundamentals
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
List in Python
List in PythonList in Python
List in Python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
12 SQL
12 SQL12 SQL
12 SQL
 
Python ppt
Python pptPython ppt
Python ppt
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 

Similar to CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file

Similar to CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file (20)

pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
Python data file handling
Python data file handlingPython data file handling
Python data file handling
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
FILE HANDLING.pptx
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptx
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File Handling
File HandlingFile Handling
File Handling
 
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
 
Data file handling in python introduction,opening &amp; closing files
Data file handling in python introduction,opening &amp; closing filesData file handling in python introduction,opening &amp; closing files
Data file handling in python introduction,opening &amp; closing files
 
DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
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
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
POWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptx
POWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptxPOWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptx
POWER POINT FILE INPUT AND OUTPUT PRESENTATION.pptx
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
presentation_files_1451938150_140676.ppt
presentation_files_1451938150_140676.pptpresentation_files_1451938150_140676.ppt
presentation_files_1451938150_140676.ppt
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 

CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file

  • 1. CBSE CLASS 12 Ch-5 File Handling SasikalaJayaprakash
  • 2. INTRODUTION  A file in itself is bunch of byes stored on some storage device like hard –drive etc .  Files help in storing information permanently .
  • 3. TEXT FILES • A text file store information in ASC11 or unicode characters • line of text is terminated,(delimited )with a special character known EOL (end of line) • Default mode • Extn of text file is .txt • Translation occur
  • 5. Opening position of file pointer File modes Opening position of file pointer r , rb , r+ , rb+ , r+b Beginning of file W, wb, w+, wb+, w+b Beginning of file a+, ab , a+ , ab+, a+b End of file if file exist otherwise beginning of file
  • 6. Opening files <file _object name> =open (<file name >) <file_ object name> =open(<file name >,<mode>) with open <file_name > as <file_object with open (‘a.txt’ , ‘r’ ) as myfile myfile = open (‘a.txt’, ‘ r’ )
  • 7. READING FILES METHOD SYNTAX DESCRIPTION Read() <filehandle>. read([n]) Read at most n bytes; if no n is specified , reads the entire file , return form of string Readline() <filehandle>.readline([n]) Reads a line of input , if n is specified reads at most n bytes, returns in form of string Readlines() <filehandle> . readlines() Read all lines and return in a list
  • 8. Writing onto files Method Syntax Description write () <file handle>.write(str1) writes string str1 to file referenced by <file handle> writelines() <file handle>.writeline(L) write all strings in list L as lines to file referenced by <filehandle>
  • 9. Standard Input ,Output and error stream • Standard input device( stdin ) – reads from the keyboard • Standard output device (stdout) – prints to the display and can redirected as standard input • Standard error device(stderr) – same as stdout but normally only for errors
  • 10. Binary files  A binary file is just a file a that contain information in the same format in which the information is held in memory  There is no delimiter for a line  No translations occur required in binary files  Faster and easier for a program to read and write  Extn is .dat file
  • 11. Pickling and unpickling • pickling refers to the process of converting the structure(such as list or dict ) to a byte stream before writing to the file . Structure(list or dictionary) Pickling Byte stream
  • 12. pickle.dump () Used to write object into file syntax : pickle.dump(<structure>,fileobject) file object is the file handle of the file where you write the contents
  • 13. pickle.load() • used to read data from file • syntax : structure=pickle.load(fileobject)
  • 14. Load() import pickle def read(): f1=open('s2.dat','rb') try: while True: rec=pickle.load(f1) print(rec) except EOFError: f1.close() read()
  • 15. Dump() import pickle def write(): f1=open('s2.dat','wb') rec=[ ] while True mark1,mark2,mark3=0,0,0 r=int(input("enter rollno:")) n=input("enter name:") m1=int(input("enter mark1:")) m2=int(input("enter mark2:")) m3=int(input("enter mark3:")) a=(m1+m2+m3)/3 lst=[r,n,m1,m2,m3,a] rec.append(lst) pickle.dump(rec,f1) f1.close() write()
  • 16. CSV Files • CSV files are comma seperated values , each line of the file is data record .Each record consist of one or more fields seperated by commas .Data stores in the form of rows and columns • It is a text file • It is mainly useful in importing and exporting excel files into csv files and processing in python • File object , csv object to be created • Accessing is fast • Can store large amount of data • Extn is .csv
  • 17. Reader () - read from the file Syntax csv.reader (file , delimiter) Default delimiter comma(,) Delimiter can be of colon( : ),semicolon (:),tab (t), pipe (|)) Writer() – write into the file syntax csv.writer(file)
  • 18. Writer() import csv f1=open('new1.csv','w',newline='') s_writer=csv.writer(f1) s_writer.writerow(["Name","Location "]) rec=[ ] while True: name=input("enter name:") Location=input("enter location:") lst=[name,Location] rec.append(lst) ch=input("do u want to enter more records y/n: ") if ch=='n': break s_writer.writerows(rec) f1.close()