SlideShare a Scribd company logo
FILE HANDLING
File Handling
A file is a sequence of bytes on
the disk/permanent storage
where a group of related data is
stored.
File is created for permanent
storage of data.
File Handling
Types of File
There are two types of files:
Text Files- A file whose contents can be viewed using a text editor is
called a text
file. (.txt)
• A text file is simply a sequence of ASCII or Unicode characters.
• EOL (new line character i.e. enter) or internal translation occurs
• e.g. Python programs, contents written in text editors
Binary Files-(.dat)
• A binary file stores the data in the same way as as stored in the memory.
• No EOL or internal translation occurs( not converted into other form
becoz it is converted into computer understandable form i.e. in binary
format)
• Best way to store program information.
. e.g. exe files,mp3 file, image files, word documents
we can’t read a binary file using a text editor.
TEXT FILE
•Text files don’t have any specific encoding
and it can be opened in
normal text editor itself.
•Example:
•Documents: txt, RTF etc.
•Tabular data: csv, tsv etc.
BINARY FILE
• Non human readable
• Can open in normal text editor.
• but we can’t read the content present inside the file.
• That’s because all the binary files will be encoded in the
binary format, which can be understood only by a computer or
machine.
• For handling such binary files we need a specific type of
software to open it.
• For Example, We need Microsoft word software to open .doc
binary files.
• Likewise, you need a pdf reader software to open .pdf binary
files and you need a photo editor software to read the
image files and so on.
File Handling
File operations- adding, modifying, deleting, reading, writing,
appending data There are three steps to perform these operations:
 Open the file.- Before any reading or writing operation of any
file , it
must be opened first
of all.
 Process file- i.e perform read or write operation.
 Close the file.-Once we are done working with the file, we should
close the
file. Closing a file releases valuable system resources.
In case we forgot to close the file, Python automatically close the file
when
program ends or file object is no longer referenced in the
program.
However, if our program is large and we are reading or writing multiple files
that can take significant amount of resource on the system. If we keep
opening
new files carelessly, we could run out of resources.
File Handling
open () - built in function
Syntax
file_object/file_handler = open(<file_name>, <access_mode>,< buffering>)
file_name = name of the file ,enclosed in double quotes.
access_mode= It is also called file mode. Determines the what
kind
of operations can be performed with file,like read,write etc.
If no mode is specified then the file will open in read mode.
File Handling
Opening file
F=open(“notes.txt”,”r”) #open a file in read mode and specified relative
path
F1=open(r“c:usershpnotes.txt”,”r”)
#open a file in read mode and specified absolute path(if file is stored in some other
folder/location
F1=open(“c:usershpnotes.txt”,”w”)
To specify absolute path of the file either use  in
each subpath or use r before the path , then
python environment will consider it as a raw path
string nothing else
F1=open(r “c:usershpnotes.txt”,”r”)
This ‘r’ has no relation with file
mode
File Handling
File opening modes-
Sr.
No
Mode & Description
1 r - reading only.Sets file pointer at beginning of the file . This is the defaultmode.
2 rb – same as r mode but with binary file
3 r+ - both reading and writing. The file pointer placed at the beginning of the file.
4 rb+ - same as r+ mode but with binary file
5 w - writing only. Overwrites the file if the file exists. If not, creates a new file for writing.
6 wb – same as w mode but with binary file.
7 w+ - both writing and reading. Overwrites . If no file exist, creates a new file for R & W.
8 wb+ - same as w+ mode but with binary file.
9 a -for appending. Move file pointer at end of the file.Creates new file for writing,if not exist.
10 ab – same as a but with binary file.
11 a+ - for both appending and reading. Move file pointer at end. If the file does not exist, it
creates
a new file for reading and writing.
12 ab+ - same as a+ mode but with binary mode.
File Handling
The read() Method
It reads the entire file and returnsit contents
in the form of a string.
Reads at most size bytes or less if end of
file occurs.
if size not mentioned then read the entire file
contents.
f=open(“notes.txt”,”r
”) r=f.read()
print(r)
File Handling
Read characters from last
position
read([size]) method
It reads the no of bytes
f=open(“notes.txt”,”r
”) r=f.read(10)
print(r)
r1=f.read(15
) print(r1)
File Handling
Absolute Path
The absolute path is the full path to some place on your computer.
OR
It is the path mentioned from the top level of hierarchy.
OR
To access a given file or directory, starting from the root of the file system
For example: Absolute path: C:UsershpDesktopcsfunction.py
Path-
it is a sequence which gives us access to a file.
•
• It is an address or location
Relative Path
The relative path is the path to some file with respect to current working
directory
e.g. Relative path: “function.py”
or
“..function.py”
File Handling
The read() Method
It reads the characters.It returns the read
characters as string
f=open(“notes.txt”,”r
”) r=f.read()
print(r)
File Handling
The readline() Method
It reads the line.It returns the read lines as
string
f=open(“notes.txt”,”r
”) r=f.readline()
print(r)
File Handling
The readlines() Method
It reads the lines.It returns the read lines as
list
f=open(“notes.txt”,”r
”) r=f.readlines()
print(r)
File Handling
Read first 2 lines
It reads the lines.It returns the read lines as list
f=open(“notes.txt”,”r
”) r=f.readline()
print(r)
r1=f.readline(
) print(r1)
OR
readlines([si
ze]) method-
Read no of
lines from
file if size
is
mentioned
File Handling
The close() Method
close(): Used to close an open file..
f=open(“notes.txt”,”r”)
r=f.read()
print(r)
f.close()
• After using this method, an opened file will
be closed and a closed file cannot be read or
written any more.
File Handling
Program to display number of lines in a
file.
f=open(“c:usershp
notes.txt”,”r”)
r=f.readlines(
) d=len(r)
print(d)
f.close()
File Handling
write() and read() based program
f = open("a.txt", 'w')
line1 = 'Welcome to python'
f.write(line1)
line2="nRegularly visit pythonapsdk.blogspot.com"
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.read()
print(text)
f.close()
OUTPUT
Welcome to
python
Regularly visit
pythonapsdk.blo
gspot.com
File Handling
Append content to a File
f = open("a.txt", 'w')
line = 'Welcome to
pythonapsdk.blogspot.com’
f.write(line)
f.close()
f = open("a.txt",
'a+') f.write("
nthanks") f.close()
f = open("a.txt",
'r') text = f.read()
print(text)
f.close()
A
P
P
E
N
D
C
O
D
E
File Handling
FLUSH()
It forces the writing of data on disc still pending in
buffer
f = open("a.txt", 'w')
line = 'Welcome to pythonapsdk.blogspot.com’
f.flush()
D=“class
xii”
f.write(D)
f.write(“sect
ion L”)
f.flush()
f.close()
File Handling
File Pointer
It tells the current position in the file
where writing or reading
will take place.(like a bookmark in a
book)
The tell() method of python tells us the current
position within the file,where as The seek(offset[,
from]) method changes the current file position. If
from is 0, the beginning of the file to seek. If it is set
to 1, the current position is used . If it is set to 2 then
the end of the file would be taken as seek
position. The offset argument indicates the number
of bytes to be moved.
f = open("a.txt", 'w')
line = 'Welcome to
pythonapsdk.blogspot.com' f.write(line)
f.close()
f = open("a.txt", 'rb+')
print(f.tell())
print(f.read(7)) # read seven characters
print(f.tell())
print(f.read())
print(f.tell())
f.seek(9,0) # moves to 9 position from
begining
print(f.read(5))
f.seek(4, 1) # moves to 4 position from
current location
print(f.read(5))
f.seek(-5, 2) # Go to the 5th byte before the
end print(f.read(5))
File handling
File Handling
With statement- no need to call
close()
With open(“notes.txt”,”w”) as f:
f.write(“hello
world”)

More Related Content

Similar to this is about file concepts in class 12 in python , text file, binary file, csv file (20)

Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
ARYAN552812
 
file handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popafile handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
Ashwini Raut
 
Data File Handling in Python Programming
Data File Handling in Python ProgrammingData File Handling in Python Programming
Data File Handling in Python Programming
gurjeetjuneja
 
FILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer ScienceFILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
Python file handling
Python file handlingPython file handling
Python file handling
Prof. Dr. K. Adisesha
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
Praveen M Jigajinni
 
FILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptxFILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptx
ssuserd0df33
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
BMS Institute of Technology and Management
 
23CS101T PSPP python program - UNIT 5.pdf
23CS101T PSPP python program - UNIT 5.pdf23CS101T PSPP python program - UNIT 5.pdf
23CS101T PSPP python program - UNIT 5.pdf
RajeshThanikachalam
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
nitamhaske
 
DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
AmitKaur17
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
RohitKurdiya1
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
Arpana Awasthi
 
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
samkinggraphics19
 
01 file handling for class use class pptx
01 file handling for class use class pptx01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
botin17097
 
file handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popafile handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
Ashwini Raut
 
Data File Handling in Python Programming
Data File Handling in Python ProgrammingData File Handling in Python Programming
Data File Handling in Python Programming
gurjeetjuneja
 
FILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer ScienceFILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
FILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptxFILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptx
ssuserd0df33
 
23CS101T PSPP python program - UNIT 5.pdf
23CS101T PSPP python program - UNIT 5.pdf23CS101T PSPP python program - UNIT 5.pdf
23CS101T PSPP python program - UNIT 5.pdf
RajeshThanikachalam
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
nitamhaske
 
DFH PDF-converted.pptx
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
AmitKaur17
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
RohitKurdiya1
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
Arpana Awasthi
 
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
samkinggraphics19
 
01 file handling for class use class pptx
01 file handling for class use class pptx01 file handling for class use class pptx
01 file handling for class use class pptx
PreeTVithule1
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
botin17097
 

Recently uploaded (20)

Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Ad

this is about file concepts in class 12 in python , text file, binary file, csv file

  • 2. File Handling A file is a sequence of bytes on the disk/permanent storage where a group of related data is stored. File is created for permanent storage of data.
  • 3. File Handling Types of File There are two types of files: Text Files- A file whose contents can be viewed using a text editor is called a text file. (.txt) • A text file is simply a sequence of ASCII or Unicode characters. • EOL (new line character i.e. enter) or internal translation occurs • e.g. Python programs, contents written in text editors Binary Files-(.dat) • A binary file stores the data in the same way as as stored in the memory. • No EOL or internal translation occurs( not converted into other form becoz it is converted into computer understandable form i.e. in binary format) • Best way to store program information. . e.g. exe files,mp3 file, image files, word documents we can’t read a binary file using a text editor.
  • 4. TEXT FILE •Text files don’t have any specific encoding and it can be opened in normal text editor itself. •Example: •Documents: txt, RTF etc. •Tabular data: csv, tsv etc.
  • 5. BINARY FILE • Non human readable • Can open in normal text editor. • but we can’t read the content present inside the file. • That’s because all the binary files will be encoded in the binary format, which can be understood only by a computer or machine. • For handling such binary files we need a specific type of software to open it. • For Example, We need Microsoft word software to open .doc binary files. • Likewise, you need a pdf reader software to open .pdf binary files and you need a photo editor software to read the image files and so on.
  • 6. File Handling File operations- adding, modifying, deleting, reading, writing, appending data There are three steps to perform these operations:  Open the file.- Before any reading or writing operation of any file , it must be opened first of all.  Process file- i.e perform read or write operation.  Close the file.-Once we are done working with the file, we should close the file. Closing a file releases valuable system resources. In case we forgot to close the file, Python automatically close the file when program ends or file object is no longer referenced in the program. However, if our program is large and we are reading or writing multiple files that can take significant amount of resource on the system. If we keep opening new files carelessly, we could run out of resources.
  • 7. File Handling open () - built in function Syntax file_object/file_handler = open(<file_name>, <access_mode>,< buffering>) file_name = name of the file ,enclosed in double quotes. access_mode= It is also called file mode. Determines the what kind of operations can be performed with file,like read,write etc. If no mode is specified then the file will open in read mode.
  • 8. File Handling Opening file F=open(“notes.txt”,”r”) #open a file in read mode and specified relative path F1=open(r“c:usershpnotes.txt”,”r”) #open a file in read mode and specified absolute path(if file is stored in some other folder/location F1=open(“c:usershpnotes.txt”,”w”) To specify absolute path of the file either use in each subpath or use r before the path , then python environment will consider it as a raw path string nothing else F1=open(r “c:usershpnotes.txt”,”r”) This ‘r’ has no relation with file mode
  • 9. File Handling File opening modes- Sr. No Mode & Description 1 r - reading only.Sets file pointer at beginning of the file . This is the defaultmode. 2 rb – same as r mode but with binary file 3 r+ - both reading and writing. The file pointer placed at the beginning of the file. 4 rb+ - same as r+ mode but with binary file 5 w - writing only. Overwrites the file if the file exists. If not, creates a new file for writing. 6 wb – same as w mode but with binary file. 7 w+ - both writing and reading. Overwrites . If no file exist, creates a new file for R & W. 8 wb+ - same as w+ mode but with binary file. 9 a -for appending. Move file pointer at end of the file.Creates new file for writing,if not exist. 10 ab – same as a but with binary file. 11 a+ - for both appending and reading. Move file pointer at end. If the file does not exist, it creates a new file for reading and writing. 12 ab+ - same as a+ mode but with binary mode.
  • 10. File Handling The read() Method It reads the entire file and returnsit contents in the form of a string. Reads at most size bytes or less if end of file occurs. if size not mentioned then read the entire file contents. f=open(“notes.txt”,”r ”) r=f.read() print(r)
  • 11. File Handling Read characters from last position read([size]) method It reads the no of bytes f=open(“notes.txt”,”r ”) r=f.read(10) print(r) r1=f.read(15 ) print(r1)
  • 12. File Handling Absolute Path The absolute path is the full path to some place on your computer. OR It is the path mentioned from the top level of hierarchy. OR To access a given file or directory, starting from the root of the file system For example: Absolute path: C:UsershpDesktopcsfunction.py Path- it is a sequence which gives us access to a file. • • It is an address or location Relative Path The relative path is the path to some file with respect to current working directory e.g. Relative path: “function.py” or “..function.py”
  • 13. File Handling The read() Method It reads the characters.It returns the read characters as string f=open(“notes.txt”,”r ”) r=f.read() print(r)
  • 14. File Handling The readline() Method It reads the line.It returns the read lines as string f=open(“notes.txt”,”r ”) r=f.readline() print(r)
  • 15. File Handling The readlines() Method It reads the lines.It returns the read lines as list f=open(“notes.txt”,”r ”) r=f.readlines() print(r)
  • 16. File Handling Read first 2 lines It reads the lines.It returns the read lines as list f=open(“notes.txt”,”r ”) r=f.readline() print(r) r1=f.readline( ) print(r1) OR readlines([si ze]) method- Read no of lines from file if size is mentioned
  • 17. File Handling The close() Method close(): Used to close an open file.. f=open(“notes.txt”,”r”) r=f.read() print(r) f.close() • After using this method, an opened file will be closed and a closed file cannot be read or written any more.
  • 18. File Handling Program to display number of lines in a file. f=open(“c:usershp notes.txt”,”r”) r=f.readlines( ) d=len(r) print(d) f.close()
  • 19. File Handling write() and read() based program f = open("a.txt", 'w') line1 = 'Welcome to python' f.write(line1) line2="nRegularly visit pythonapsdk.blogspot.com" f.write(line2) f.close() f = open("a.txt", 'r') text = f.read() print(text) f.close() OUTPUT Welcome to python Regularly visit pythonapsdk.blo gspot.com
  • 20. File Handling Append content to a File f = open("a.txt", 'w') line = 'Welcome to pythonapsdk.blogspot.com’ f.write(line) f.close() f = open("a.txt", 'a+') f.write(" nthanks") f.close() f = open("a.txt", 'r') text = f.read() print(text) f.close() A P P E N D C O D E
  • 21. File Handling FLUSH() It forces the writing of data on disc still pending in buffer f = open("a.txt", 'w') line = 'Welcome to pythonapsdk.blogspot.com’ f.flush() D=“class xii” f.write(D) f.write(“sect ion L”) f.flush() f.close()
  • 22. File Handling File Pointer It tells the current position in the file where writing or reading will take place.(like a bookmark in a book) The tell() method of python tells us the current position within the file,where as The seek(offset[, from]) method changes the current file position. If from is 0, the beginning of the file to seek. If it is set to 1, the current position is used . If it is set to 2 then the end of the file would be taken as seek position. The offset argument indicates the number of bytes to be moved.
  • 23. f = open("a.txt", 'w') line = 'Welcome to pythonapsdk.blogspot.com' f.write(line) f.close() f = open("a.txt", 'rb+') print(f.tell()) print(f.read(7)) # read seven characters print(f.tell()) print(f.read()) print(f.tell()) f.seek(9,0) # moves to 9 position from begining print(f.read(5)) f.seek(4, 1) # moves to 4 position from current location print(f.read(5)) f.seek(-5, 2) # Go to the 5th byte before the end print(f.read(5)) File handling
  • 24. File Handling With statement- no need to call close() With open(“notes.txt”,”w”) as f: f.write(“hello world”)