SlideShare a Scribd company logo
JINI N K
FileHandling2023_Text File.pdf CBSE 2014
WHY DO WE NEED FILE HANDLING?
 Files are used to store data
permanently
FILE
 A file is a bunch of bytes stored on some
storage device
 Devices like hard disk, thumb drive etc.
 Can be stored permanently and can be
retrieved
TYPES OF FILES
TEXT FILE
BINARY FILE
CSV FILE
TEXT FILES
 Stores information in ASCII or Unicode characters
 Each line of text is terminated with a special
character known as EOL – End Of Line character
(‘n’, ‘r n’)
 Internal translations take place when EOL is read
or written
 Extension for text files is .txt
 Default mode of file
TEXT FILE
DELIMITED TEXT
FILE
REGULAR TEXT
FILE
TSV FILE CSV FILE
BINARY FILES
 Sores information in the form of a stream of bytes
 Contains information in the same format in which
the information is held in memory
 No delimiter for a line
 No translations required
 Faster and easier than text files
 More secure
CSV FILES
 Comma Separated Values (CSV) file
 Simple file in human readable format
 Used to store tabular data in plain text
 Delimiter is usually a comma
 Easy to generate and import onto a spreadsheet or
database
TEXT FILE VS BINARY
STEPS IN FILE HANDLING
First open the file for read or write by
specifying the name of file and mode
Opening File
Read / Write
Closing File
Once the file is opened, can either
read or write
Close the file after performing the operation
BASIC FILE MANIPULATION
File Operations
Create
Search
Add Delete
Update
‫ﷺ‬ Reading data from files
‫ﷺ‬ Writing data to files
‫ﷺ‬ Appending data to files
OPENING A TEXT FILE
<file_objectname> = open(<filename>)
OR
<file_objectname> = open(<filename>, <mode>)
File object
created
Mode of a File
Name / Path
to a File
OPENING A TEXT FILE - EXAMPLE
f = open(“Apple.txt”, ”r”)
• Here “Apple.txt” is loaded in memory and its
reference is linked to ‘f’ object.
• Python will access “Bike.txt” through ‘f’ object
• The file is opened in read mode
File object is also known as File handle
ACCESSING A FILE IN DIFFERENT LOCATION
• Opens file “story.txt” and attaches it to the file object f
• The slashes in the path must be doubled
f = open(“d:mainstory.txt”, ”r”)
F = open(r“d:mydatapoem.txt”,”r”)
• Another solution of double backslash is using “r” before the
path making the string as raw string
i.e. no special meaning attached to any character
OPENING FILES
• The default file-open mode is read mode
• If mode is not specified, Python will open in read mode
• When you open a file in read mode, the given file must
exist in the folder, otherwise Python will raise
FileNotFoundError
FILE ACCESS MODES
‘r’
‘w’
‘a’ Append
Write Only
Read Only
• If the file does not exist, file is created.
• If the file exists, Python will truncate existing
data and overwrite the file
• New data will be added to the end of
existing data, no overwriting
• If file does not exist, Python will create a
new file
Filemust exist already,otherwise Python
raises I/O errors
Read and Write
Write and Read
Append and
Read
‘r+’
‘w+’
‘a+’
• File is created if not exists
• If exists data is retained; new data is
appended
• Both read and write allowed
• File is created if not exists
• If exists data will be truncated
• Both read and write allowed
• File must exists otherwise error is raised
• Both reading and writing can take place
CLOSING A FILE
An opened file is closed by calling the close() method
Syntax: <fileHandle>.close()
Example: f.close()
READING FROM FILES
‫ﷺ‬ read()
‫ﷺ‬ readline()
‫ﷺ‬ readlines()
CREATING A TEXT FILE
Open any text editor,
and type any content
Copy the path from
where your python is
installed
Save your file in the
path which is copied
with .txt extension
1
2 3
read()
• Reads at most n bytes
• If no n is specified, reads the entire file
• Reads the bytes in the form of a string
Syntax: <fileHandle>.read([n])
f.read()
Code Snippet:
Output 
Reading a file’s entire content
 Code Snippet
Output 
Reading a file’s first 30 bytes and printing it.
f.read(n)
 Code Snippet
Output 
Reading n bytes and then reading some
more bytes from the last position read.
f.read(n)
readline()
• Reads a line of input
• If n is specified, reads at most n bytes
• Returns a blank string if no more bytes are left for
reading in the file
• Reads the bytes in the form of a string
Syntax:
<fileHandle>.readline([n])
Output:
Reading a file’s first line
Code Snippet:
Output:
Reading a file’s first three lines – line by line
Code Snippet:
Output:
Reading a file’s first three lines – line by line
Code Snippet:
Output:
Reading a file using readline(n)
Code Snippet:
Output:
Reading a complete file line by line
Code Snippet:
Output:
Reading a file line by line using for loop
Code Snippet:
Output:
Reading a file line by line using for loop
Code Snippet:
Code Snippet: Output:
Displaying the size of a file after removing EOL
characters, white spaces & blank lines
readlines()
• Reads all lines
• Returns them in a list
Syntax: <fileHandle>.readlines()
Output:
Reading a complete file in a list
Code Snippet 
Output:
Program to display the size of a file in bytes
Code Snippet:
Output:
Program to display the number of lines in a file
Code Snippet:
WRITING ONTO FILES
‫ﷺ‬ write()
‫ﷺ‬ writelines()
• If file doesn’t exists, it will create a new file
• If file exists, it will write the data into the file
write()
• write() method takes a string and writes it in the file
• For storing data, with EOL character, we have to add ‘n’
character to the end of the string
• For storing numeric value, we have to either convert it into
string using str() or write in quotes
Syntax: <fileHandle>.write(str)
writelines(seq)
writelines() method is used to write sequence
data types in a file (string, list.)
Syntax:
<fileHandle>.writelines(L)
APPENDING DATA TO A FILE
• Append means to add the data at the end of file
using ‘a’ mode
• If file does not exists, it will create the new file
• If file exists, it will append/add the data at the
end
of the file
Output 
Create a text file to hold some data
Code Snippet 
Output 
Create a text file to hold some data
Code Snippet 
Text File :
Create a text file to hold student names
Code Snippet :
Input :
Text File :
Create a text file to hold student names in different lines
Code Snippet :
Input :
Text File :
Create a file with some names separated by newline characters
without using write() function
Code Snippet :
Input :
Text File :
Create a file with some names separated by newline characters
without using write() function
Input :
Program to get roll numbers, names and marks of the students
of a class and store these details in a file called ‘Marls.txt’
Code Snippet :
Text File :
Input :
Program to add two more students details to the file Marks.txt
created earlier
Code Snippet :
Text File :
Input :
Program to display the contents of file ‘Marks.txt’ which is
created earlier
OR
Code Snippet : Output:
Program to read a text file line by line and display each
word separated by a ‘#’
Code Snippet : Output:
Program to read a text file line by line and display the
counts of vowels and consonants in the file.
Code Snippet : Output:
JINI N K

More Related Content

Similar to FileHandling2023_Text File.pdf CBSE 2014 (20)

PPTX
Unit V.pptx
ShaswatSurya
 
PPTX
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
PPTX
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
PDF
Python file handling
Prof. Dr. K. Adisesha
 
PPTX
Chapter 08 data file handling
Praveen M Jigajinni
 
PDF
Python reading and writing files
Mukesh Tekwani
 
PPTX
DFH PDF-converted.pptx
AmitKaur17
 
PDF
File handling with python class 12th .pdf
lionsconvent1234
 
PDF
lecs102.pdf kjolholhkl';l;llkklkhjhjbhjjmnj
MrProfEsOr1
 
PPTX
FILE INPUT OUTPUT.pptx
ssuserd0df33
 
PPTX
01 file handling for class use class pptx
PreeTVithule1
 
PPTX
for interview this ppt is a teching aid for file handling concepts includes t...
Primary2Primary2
 
PPTX
Data File Handling in Python Programming
gurjeetjuneja
 
PPTX
VKS-Python-FIe Handling text CSV Binary.pptx
Vinod Srivastava
 
PDF
File handling in Python
BMS Institute of Technology and Management
 
PPTX
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
RohitKurdiya1
 
PPTX
file handling in python using exception statement
srividhyaarajagopal
 
PPTX
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
PPTX
pspp-rsk.pptx
ARYAN552812
 
PPT
Python File functions
keerthanakommera1
 
Unit V.pptx
ShaswatSurya
 
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
Python file handling
Prof. Dr. K. Adisesha
 
Chapter 08 data file handling
Praveen M Jigajinni
 
Python reading and writing files
Mukesh Tekwani
 
DFH PDF-converted.pptx
AmitKaur17
 
File handling with python class 12th .pdf
lionsconvent1234
 
lecs102.pdf kjolholhkl';l;llkklkhjhjbhjjmnj
MrProfEsOr1
 
FILE INPUT OUTPUT.pptx
ssuserd0df33
 
01 file handling for class use class pptx
PreeTVithule1
 
for interview this ppt is a teching aid for file handling concepts includes t...
Primary2Primary2
 
Data File Handling in Python Programming
gurjeetjuneja
 
VKS-Python-FIe Handling text CSV Binary.pptx
Vinod Srivastava
 
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
RohitKurdiya1
 
file handling in python using exception statement
srividhyaarajagopal
 
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
pspp-rsk.pptx
ARYAN552812
 
Python File functions
keerthanakommera1
 

Recently uploaded (20)

DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Hashing Introduction , hash functions and techniques
sailajam21
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
MRRS Strength and Durability of Concrete
CivilMythili
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Ad

FileHandling2023_Text File.pdf CBSE 2014

  • 3. WHY DO WE NEED FILE HANDLING?  Files are used to store data permanently
  • 4. FILE  A file is a bunch of bytes stored on some storage device  Devices like hard disk, thumb drive etc.  Can be stored permanently and can be retrieved
  • 5. TYPES OF FILES TEXT FILE BINARY FILE CSV FILE
  • 6. TEXT FILES  Stores information in ASCII or Unicode characters  Each line of text is terminated with a special character known as EOL – End Of Line character (‘n’, ‘r n’)  Internal translations take place when EOL is read or written  Extension for text files is .txt  Default mode of file
  • 7. TEXT FILE DELIMITED TEXT FILE REGULAR TEXT FILE TSV FILE CSV FILE
  • 8. BINARY FILES  Sores information in the form of a stream of bytes  Contains information in the same format in which the information is held in memory  No delimiter for a line  No translations required  Faster and easier than text files  More secure
  • 9. CSV FILES  Comma Separated Values (CSV) file  Simple file in human readable format  Used to store tabular data in plain text  Delimiter is usually a comma  Easy to generate and import onto a spreadsheet or database
  • 10. TEXT FILE VS BINARY
  • 11. STEPS IN FILE HANDLING First open the file for read or write by specifying the name of file and mode Opening File Read / Write Closing File Once the file is opened, can either read or write Close the file after performing the operation
  • 12. BASIC FILE MANIPULATION File Operations Create Search Add Delete Update ‫ﷺ‬ Reading data from files ‫ﷺ‬ Writing data to files ‫ﷺ‬ Appending data to files
  • 13. OPENING A TEXT FILE <file_objectname> = open(<filename>) OR <file_objectname> = open(<filename>, <mode>) File object created Mode of a File Name / Path to a File
  • 14. OPENING A TEXT FILE - EXAMPLE f = open(“Apple.txt”, ”r”) • Here “Apple.txt” is loaded in memory and its reference is linked to ‘f’ object. • Python will access “Bike.txt” through ‘f’ object • The file is opened in read mode File object is also known as File handle
  • 15. ACCESSING A FILE IN DIFFERENT LOCATION • Opens file “story.txt” and attaches it to the file object f • The slashes in the path must be doubled f = open(“d:mainstory.txt”, ”r”) F = open(r“d:mydatapoem.txt”,”r”) • Another solution of double backslash is using “r” before the path making the string as raw string i.e. no special meaning attached to any character
  • 16. OPENING FILES • The default file-open mode is read mode • If mode is not specified, Python will open in read mode • When you open a file in read mode, the given file must exist in the folder, otherwise Python will raise FileNotFoundError
  • 17. FILE ACCESS MODES ‘r’ ‘w’ ‘a’ Append Write Only Read Only • If the file does not exist, file is created. • If the file exists, Python will truncate existing data and overwrite the file • New data will be added to the end of existing data, no overwriting • If file does not exist, Python will create a new file Filemust exist already,otherwise Python raises I/O errors
  • 18. Read and Write Write and Read Append and Read ‘r+’ ‘w+’ ‘a+’ • File is created if not exists • If exists data is retained; new data is appended • Both read and write allowed • File is created if not exists • If exists data will be truncated • Both read and write allowed • File must exists otherwise error is raised • Both reading and writing can take place
  • 19. CLOSING A FILE An opened file is closed by calling the close() method Syntax: <fileHandle>.close() Example: f.close()
  • 20. READING FROM FILES ‫ﷺ‬ read() ‫ﷺ‬ readline() ‫ﷺ‬ readlines()
  • 21. CREATING A TEXT FILE Open any text editor, and type any content Copy the path from where your python is installed Save your file in the path which is copied with .txt extension 1 2 3
  • 22. read() • Reads at most n bytes • If no n is specified, reads the entire file • Reads the bytes in the form of a string Syntax: <fileHandle>.read([n])
  • 23. f.read() Code Snippet: Output  Reading a file’s entire content
  • 24.  Code Snippet Output  Reading a file’s first 30 bytes and printing it. f.read(n)
  • 25.  Code Snippet Output  Reading n bytes and then reading some more bytes from the last position read. f.read(n)
  • 26. readline() • Reads a line of input • If n is specified, reads at most n bytes • Returns a blank string if no more bytes are left for reading in the file • Reads the bytes in the form of a string Syntax: <fileHandle>.readline([n])
  • 27. Output: Reading a file’s first line Code Snippet:
  • 28. Output: Reading a file’s first three lines – line by line Code Snippet:
  • 29. Output: Reading a file’s first three lines – line by line Code Snippet:
  • 30. Output: Reading a file using readline(n) Code Snippet:
  • 31. Output: Reading a complete file line by line Code Snippet:
  • 32. Output: Reading a file line by line using for loop Code Snippet:
  • 33. Output: Reading a file line by line using for loop Code Snippet:
  • 34. Code Snippet: Output: Displaying the size of a file after removing EOL characters, white spaces & blank lines
  • 35. readlines() • Reads all lines • Returns them in a list Syntax: <fileHandle>.readlines()
  • 36. Output: Reading a complete file in a list Code Snippet 
  • 37. Output: Program to display the size of a file in bytes Code Snippet:
  • 38. Output: Program to display the number of lines in a file Code Snippet:
  • 39. WRITING ONTO FILES ‫ﷺ‬ write() ‫ﷺ‬ writelines() • If file doesn’t exists, it will create a new file • If file exists, it will write the data into the file
  • 40. write() • write() method takes a string and writes it in the file • For storing data, with EOL character, we have to add ‘n’ character to the end of the string • For storing numeric value, we have to either convert it into string using str() or write in quotes Syntax: <fileHandle>.write(str)
  • 41. writelines(seq) writelines() method is used to write sequence data types in a file (string, list.) Syntax: <fileHandle>.writelines(L)
  • 42. APPENDING DATA TO A FILE • Append means to add the data at the end of file using ‘a’ mode • If file does not exists, it will create the new file • If file exists, it will append/add the data at the end of the file
  • 43. Output  Create a text file to hold some data Code Snippet 
  • 44. Output  Create a text file to hold some data Code Snippet 
  • 45. Text File : Create a text file to hold student names Code Snippet : Input :
  • 46. Text File : Create a text file to hold student names in different lines Code Snippet : Input :
  • 47. Text File : Create a file with some names separated by newline characters without using write() function Code Snippet : Input :
  • 48. Text File : Create a file with some names separated by newline characters without using write() function Input :
  • 49. Program to get roll numbers, names and marks of the students of a class and store these details in a file called ‘Marls.txt’ Code Snippet :
  • 51. Program to add two more students details to the file Marks.txt created earlier Code Snippet :
  • 53. Program to display the contents of file ‘Marks.txt’ which is created earlier OR Code Snippet : Output:
  • 54. Program to read a text file line by line and display each word separated by a ‘#’ Code Snippet : Output:
  • 55. Program to read a text file line by line and display the counts of vowels and consonants in the file. Code Snippet : Output: