SlideShare a Scribd company logo
1 of 16
Department of Computer Science & Engineering
SUBMITTED BY:
Ishant Jaiswal
2201920100146
CSE 2nd YEAR
SUBMITTED TO:
MR. MEGH SINGHAL
ASSISTANT PROFESSOR
CSE DEPARTMENT
FILE HANDLING IN PYTHON
What is file?
• Files are named locations on disk to information.
• They are used to store data permanently.
• Data is stored in non-volatile memory.
• We can retrieve data whenever
required.
Types of
file?
Text files:-
• Store data in the form of character. It is used
to store data and string. ex-txt file.
Binary files:-
• Store data in the form of bytes(group of 8
bits). ex-audio file,video file,pdf file.
What is file handling?
File handling means:-
• Opening a file.
• Performing some operations on it.
• Closing a file.
Opening file:-
Python provides an in built function open() to open file.
• file name:- file to be accessed.
• mode:- access mode (purpose of opening file).
• f :- file handler, file pointer.
• Syntax:-
f=open(file
name,mode=’r’,buffering,encoding=None,errors=None,newline=None,
closefd=True)
f=open(file name, mode=’r’)
Closing a file:-
•
• After performing operation, we have to close a file.
• Syntax:-
file_handler.close()
•
• close() :- function used to close a file.
Read data from file:-
T0 read content of file, we have following three methods:-
• read()
• readline()
• readlines()
Read data from file:-
read() :- This method is used to read data/content from a file
and return it as a string in text mode. it return bytes in binary
mode.
size:- It represent the number of character to be read in text
mode.
Syntax:-
file_object.read(size)
Read data from file:-
readline() :- This function is used to read a single line from a
file.
size:- It represent the number of character to read from line
Syntax:-
file_object.readline(size)
Read data from file:-
readlines() :- This function is used to read a all line from a
file and returns a list of lines.
Syntax:-
file_object.readlines()
Important Method:-
• tell() method
• seek() method
tell() :-
• This method is used to find the current position of a
file pointer from the beginning of the file.
• Position start from 0. It just like indexing in string.
Syntax:-
file_object.tell()
Important Method:-
seek() :-
• This method is used to change the position of file pointer.
• Remember file pointer position is always counted from
the beginning.
Syntax:-
file_object.seek(position)
ishant.txt
Name:- Ishant Jaiswal
Roll No:- 2201920100146
Read data
Writing in a file:-
• In order to write data in file, we have to open in a mode
which provides the facility of writing data ex. w,a,x etc.
• W mode:-
• It opens the file to write only
• It overwrite the data in a file.
• If a file doesn’t exist, it creates a new file and write into it
• Mainly, two method are used for writing data in files
• write()
• writelines()
Writing in a file:-
• Syntax:-
file_object.write(data in string format)
ishant.txt
Name:- Ishant Jaiswal
Roll No:- 2201920100146
Write data

More Related Content

Similar to File handling & basic operation on file in python

File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4Prerna Sharma
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceanuvayalil5525
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugevsol7206
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programmingSrinivas Narasegouda
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptRaja Ram Dutta
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Filesprimeteacher32
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with exampleSunil Patel
 

Similar to File handling & basic operation on file in python (20)

Data file handling
Data file handlingData file handling
Data file handling
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
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
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
working with files
working with filesworking with files
working with files
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
05io
05io05io
05io
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
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 & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programming
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Python-files
Python-filesPython-files
Python-files
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
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
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
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🔝
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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 ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
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...
 

File handling & basic operation on file in python

  • 1. Department of Computer Science & Engineering SUBMITTED BY: Ishant Jaiswal 2201920100146 CSE 2nd YEAR SUBMITTED TO: MR. MEGH SINGHAL ASSISTANT PROFESSOR CSE DEPARTMENT FILE HANDLING IN PYTHON
  • 2. What is file? • Files are named locations on disk to information. • They are used to store data permanently. • Data is stored in non-volatile memory. • We can retrieve data whenever required.
  • 3. Types of file? Text files:- • Store data in the form of character. It is used to store data and string. ex-txt file. Binary files:- • Store data in the form of bytes(group of 8 bits). ex-audio file,video file,pdf file.
  • 4. What is file handling? File handling means:- • Opening a file. • Performing some operations on it. • Closing a file.
  • 5. Opening file:- Python provides an in built function open() to open file. • file name:- file to be accessed. • mode:- access mode (purpose of opening file). • f :- file handler, file pointer. • Syntax:- f=open(file name,mode=’r’,buffering,encoding=None,errors=None,newline=None, closefd=True) f=open(file name, mode=’r’)
  • 6. Closing a file:- • • After performing operation, we have to close a file. • Syntax:- file_handler.close() • • close() :- function used to close a file.
  • 7. Read data from file:- T0 read content of file, we have following three methods:- • read() • readline() • readlines()
  • 8. Read data from file:- read() :- This method is used to read data/content from a file and return it as a string in text mode. it return bytes in binary mode. size:- It represent the number of character to be read in text mode. Syntax:- file_object.read(size)
  • 9. Read data from file:- readline() :- This function is used to read a single line from a file. size:- It represent the number of character to read from line Syntax:- file_object.readline(size)
  • 10. Read data from file:- readlines() :- This function is used to read a all line from a file and returns a list of lines. Syntax:- file_object.readlines()
  • 11. Important Method:- • tell() method • seek() method tell() :- • This method is used to find the current position of a file pointer from the beginning of the file. • Position start from 0. It just like indexing in string. Syntax:- file_object.tell()
  • 12. Important Method:- seek() :- • This method is used to change the position of file pointer. • Remember file pointer position is always counted from the beginning. Syntax:- file_object.seek(position)
  • 13. ishant.txt Name:- Ishant Jaiswal Roll No:- 2201920100146 Read data
  • 14. Writing in a file:- • In order to write data in file, we have to open in a mode which provides the facility of writing data ex. w,a,x etc. • W mode:- • It opens the file to write only • It overwrite the data in a file. • If a file doesn’t exist, it creates a new file and write into it • Mainly, two method are used for writing data in files • write() • writelines()
  • 15. Writing in a file:- • Syntax:- file_object.write(data in string format)
  • 16. ishant.txt Name:- Ishant Jaiswal Roll No:- 2201920100146 Write data