SlideShare a Scribd company logo
1 of 20
Download to read offline
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

What's hot

Fisiere Pascal/Pascal ABC
Fisiere Pascal/Pascal ABCFisiere Pascal/Pascal ABC
Fisiere Pascal/Pascal ABCm_gutu
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methodskeeeerty
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systemsvampugani
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
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
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Edureka!
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
 
File and directory
File and directoryFile and directory
File and directorySunil Kafle
 
File Management – File Concept, access methods, File types and File Operation
File Management – File Concept, access methods,  File types and File OperationFile Management – File Concept, access methods,  File types and File Operation
File Management – File Concept, access methods, File types and File OperationDhrumil Panchal
 
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 Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating SystemJanki Shah
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 

What's hot (20)

Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
Fisiere Pascal/Pascal ABC
Fisiere Pascal/Pascal ABCFisiere Pascal/Pascal ABC
Fisiere Pascal/Pascal ABC
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systems
 
File Management
File ManagementFile Management
File Management
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
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
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Case study windows
Case study windowsCase study windows
Case study windows
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
File and directory
File and directoryFile and directory
File and directory
 
File Management – File Concept, access methods, File types and File Operation
File Management – File Concept, access methods,  File types and File OperationFile Management – File Concept, access methods,  File types and File Operation
File Management – File Concept, access methods, File types and File Operation
 
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 Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
file handling c++
file handling c++file handling c++
file handling c++
 
Working with JSON
Working with JSONWorking with JSON
Working with JSON
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 

Similar to chapter-4-data-file-handlingeng.pdf

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

Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
03-01-File Handling.pdf
03-01-File Handling.pdf03-01-File Handling.pdf
03-01-File Handling.pdf
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
File management
File managementFile management
File management
 
Python-files
Python-filesPython-files
Python-files
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.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 as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
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
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
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
 
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
 
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🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
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
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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...
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

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