SlideShare a Scribd company logo
1 of 26
Download to read offline
CHAPTER : 5 FILE HANDLING
INTRODUCTION
• Files help to store the information
permanently.
• A file in itself is a bunch of bytes stored on
some storage device like hard – disk,some storage device like hard – disk,
thumb – drive, etc.,
DATA FILES
• The data files are the files that store data
pertaining to a specific application, for
later use.
• Types :• Types :
– Text Files
– Binary Files
TEXT FILES
• A text files stores information in ASCII or
characters.
• Each line of text is terminated (Delimited)
with a special character known as EOL (end
of Line) character.of Line) character.
• Some internal translations take place when
this EOL character is read or written.
• EOL character
– By default, newline character (‘n’) or carriage –
return, newline combination (‘rn’)
BINARY FILES
• A binary files is a file that contains
information in the same format in which the
information is held in memory.
• There is no delimiter for a line,
• No translations occur in binary files.• No translations occur in binary files.
• Binary files are faster and easier for a
program to read and write than are text files.
• As long as the file doesn’t need to be read by
people or need to be ported to a different type
of system, binary files are the best way to
store program information
Basic File Manipulation
• Reading data from files
• Writing data to files
• Appending data to files
Opening Files
• Syntax
ExampleExample
• A file object is also known as File Handle.
• Python’s open() function creates a file object which serves a
link to a file residing on your computer.
• The first parameter - Path to file.
• Second Parameter – Mode• Second Parameter – Mode
• r - read
• w – write
• a - append
• Single
Paths in Filename
File Object / File Handle
• File objects are used to read and write data to a file on
disk
• A file object is a reference to a file on disk. It opens and
makes it available for a number of different tasks.
• A python program can work with files stored on• A python program can work with files stored on
hardware through file object only.
File Access Mode
Closing Files
• Syntax :
• Example :
outfile =open(“Master.txt”, “r”)
• A close() function break the link of file – object and the file on the
hard disk.
• After close(), no tasks can be performed on that file through the file
– object ( or file handle)
Reading from files
• Before read from a file, be opened and
linked via a file –object or file handle.
Code -1
Combine - Open and Read
Read the first line of the Poem
Code – 2
Code 3
Code 4
readline() – read a full line. A line is considered till a new character (EOL) is
encountered in the data file
Code 5
Reading a complete file - line by line
• Using for loop
• Example• Example
strip()
• strip() – It removes leading and trialing
whitespaces
readlines()
Program
Program

More Related Content

What's hot

File Types in Data Structure
File Types in Data StructureFile Types in Data Structure
File Types in Data StructureProf Ansari
 
File organization
File organizationFile organization
File organizationGokul017
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMSkoolkampus
 
Disk allocation methods
Disk allocation methodsDisk allocation methods
Disk allocation methodsajeela mushtaq
 
Examining Mac File Structures
Examining Mac File StructuresExamining Mac File Structures
Examining Mac File Structuresprimeteacher32
 
Free Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFSFree Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFSUnited International University
 
M2 211-unix fs-rl_2.1.1
M2 211-unix fs-rl_2.1.1M2 211-unix fs-rl_2.1.1
M2 211-unix fs-rl_2.1.1MrudulaJoshi10
 
Microsoft Windows File System in Operating System
Microsoft Windows File System in Operating SystemMicrosoft Windows File System in Operating System
Microsoft Windows File System in Operating SystemMeghaj Mallick
 
Linked allocation 48
Linked  allocation 48Linked  allocation 48
Linked allocation 48myrajendra
 
14 file handling
14 file handling14 file handling
14 file handlingAPU
 
12 comp sci_revision_note_11
12 comp sci_revision_note_1112 comp sci_revision_note_11
12 comp sci_revision_note_11KaranpreetSingh73
 
Introduction to File System
Introduction to File SystemIntroduction to File System
Introduction to File SystemSanthiNivas
 
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
 
File protection.59 to 60
File protection.59 to 60File protection.59 to 60
File protection.59 to 60myrajendra
 

What's hot (20)

File handling
File handlingFile handling
File handling
 
File Types in Data Structure
File Types in Data StructureFile Types in Data Structure
File Types in Data Structure
 
File organization
File organizationFile organization
File organization
 
4 create database
4 create database4 create database
4 create database
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS
 
Disk allocation methods
Disk allocation methodsDisk allocation methods
Disk allocation methods
 
Examining Mac File Structures
Examining Mac File StructuresExamining Mac File Structures
Examining Mac File Structures
 
Ioppt
IopptIoppt
Ioppt
 
Free Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFSFree Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFS
 
M2 211-unix fs-rl_2.1.1
M2 211-unix fs-rl_2.1.1M2 211-unix fs-rl_2.1.1
M2 211-unix fs-rl_2.1.1
 
Microsoft Windows File System in Operating System
Microsoft Windows File System in Operating SystemMicrosoft Windows File System in Operating System
Microsoft Windows File System in Operating System
 
Linked allocation 48
Linked  allocation 48Linked  allocation 48
Linked allocation 48
 
14 file handling
14 file handling14 file handling
14 file handling
 
12 comp sci_revision_note_11
12 comp sci_revision_note_1112 comp sci_revision_note_11
12 comp sci_revision_note_11
 
Introduction to File System
Introduction to File SystemIntroduction to File System
Introduction to File System
 
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 protection.59 to 60
File protection.59 to 60File protection.59 to 60
File protection.59 to 60
 
File allocation methods (1)
File allocation methods (1)File allocation methods (1)
File allocation methods (1)
 
Handling computer files
Handling computer filesHandling computer files
Handling computer files
 
SSM
SSMSSM
SSM
 

Similar to File Handling in Python

File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by FaixanٖFaiXy :)
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptxqover
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in pythonnitamhaske
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdfbotin17097
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.pptxMasterDarsh
 
2nd unit part 1
2nd unit  part 12nd unit  part 1
2nd unit part 1Pavan Illa
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Filesprimeteacher32
 
Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)FerryKemperman
 
chapter-4-data-file-handlingeng.pdf
chapter-4-data-file-handlingeng.pdfchapter-4-data-file-handlingeng.pdf
chapter-4-data-file-handlingeng.pdfSyedAhmed991492
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)Ahmed El-Arabawy
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
File handing in C
File handing in CFile handing in C
File handing in Cshrishcg
 

Similar to File Handling in Python (20)

Python file handling
Python file handlingPython file handling
Python file handling
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
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
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.pptx
 
2nd unit part 1
2nd unit  part 12nd unit  part 1
2nd unit part 1
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)
 
chapter-4-data-file-handlingeng.pdf
chapter-4-data-file-handlingeng.pdfchapter-4-data-file-handlingeng.pdf
chapter-4-data-file-handlingeng.pdf
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
Embedded Systems: Lecture 14: Introduction to GNU Toolchain (Binary Utilities)
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 
File handing in C
File handing in CFile handing in C
File handing in C
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

File Handling in Python

  • 1. CHAPTER : 5 FILE HANDLING
  • 2. INTRODUCTION • Files help to store the information permanently. • A file in itself is a bunch of bytes stored on some storage device like hard – disk,some storage device like hard – disk, thumb – drive, etc.,
  • 3. DATA FILES • The data files are the files that store data pertaining to a specific application, for later use. • Types :• Types : – Text Files – Binary Files
  • 4. TEXT FILES • A text files stores information in ASCII or characters. • Each line of text is terminated (Delimited) with a special character known as EOL (end of Line) character.of Line) character. • Some internal translations take place when this EOL character is read or written. • EOL character – By default, newline character (‘n’) or carriage – return, newline combination (‘rn’)
  • 5. BINARY FILES • A binary files is a file that contains information in the same format in which the information is held in memory. • There is no delimiter for a line, • No translations occur in binary files.• No translations occur in binary files. • Binary files are faster and easier for a program to read and write than are text files. • As long as the file doesn’t need to be read by people or need to be ported to a different type of system, binary files are the best way to store program information
  • 6. Basic File Manipulation • Reading data from files • Writing data to files • Appending data to files
  • 8. • A file object is also known as File Handle. • Python’s open() function creates a file object which serves a link to a file residing on your computer. • The first parameter - Path to file. • Second Parameter – Mode• Second Parameter – Mode • r - read • w – write • a - append • Single
  • 10. File Object / File Handle • File objects are used to read and write data to a file on disk • A file object is a reference to a file on disk. It opens and makes it available for a number of different tasks. • A python program can work with files stored on• A python program can work with files stored on hardware through file object only.
  • 12. Closing Files • Syntax : • Example : outfile =open(“Master.txt”, “r”) • A close() function break the link of file – object and the file on the hard disk. • After close(), no tasks can be performed on that file through the file – object ( or file handle)
  • 13. Reading from files • Before read from a file, be opened and linked via a file –object or file handle.
  • 14.
  • 16. Combine - Open and Read
  • 17. Read the first line of the Poem
  • 20. Code 4 readline() – read a full line. A line is considered till a new character (EOL) is encountered in the data file
  • 22. Reading a complete file - line by line • Using for loop • Example• Example
  • 23. strip() • strip() – It removes leading and trialing whitespaces