SlideShare a Scribd company logo
File Handling in Python
Prof. Anand A Bhosle
Department of Information technology
International Institute of Information Technology, I²IT
www.isquareit.edu.in
File
• A file is collection of information/data in a
particular format.
• A file has different extension based on data it
stored in it and format it uses for representing
that data.
• A file is typically stored on a Storage medium
such as Disk(hard Disk).
• A file is stored on a disk at a location(path).
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
File Structure
• Files are Stored in a hierarchical structure like
a tree.
• At top of tree there is a root node.
• A root node branches into multiple partitions
or drives.
• Drives are having folders or directories and
files .
• Directories in turn may have another folders
or files
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Files
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
C:/Myfile
(Directory)
My Computer
C:(Drive)
A.txt
(Text File)
D:( Drive) E: (Drive)
File Path
• Files can be retrieved or accessed by
traversing through the hierarchical( tree)
structure as shown in above figure.
• So to access a file, we need to traverse from
root node to the node of a file.
• While traversing , we are visiting the nodes in
a sequence to file node is nothing but path or
location of a file.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
File Path
• In figure shown above three drive are root
nodes.
• A text file is stored on D drive , so we start
traversal from D node.
• To access a a.txt file , we will traverse from
node D to node a.txt .
• So the path of the a.txt is :
D:Myfilea.txt
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
File Path
“D:Myfilea.txt “ character after dot
indicates the extension of the file.
File can have different extension, example .txt,
.pdf , .docx , . pptx.
In above path character() used to separate
the folders or nodes is known as delimiter.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Absolute Path
Absolute path is also called complete or full
path.
Absolute path contains path from root node
and all directories and subdirectories that
contains a file
 D:Myfilea.txt path is the example of
absolute path as it contains all nodes from
root node to the a.txt node.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Relative Path
• A relative path starts with respect to present
working directory.
• A file can be accessed by using absolute path.
• A relative path is combined with another path
to form a complete path.
Example : C:StudentsMyFolderMyfile.txt
If C:Students is present working directory.
Then MyFoldeMyfile.txt is Relative Path
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Types of Files
Python Supports two types of files.
ASCII Text Files
Binary Files.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
ASCII Text Files
In a Text file each character represents a byte.
Each character has an ASCII value associated
with it.
Text files are processed sequentially in
forward direction.
So , text files can perform one operation at a
time(read/write).
Text files can read or write one character at a
time. International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
ASCII Text Files
A text file can contain zero or more characters.
A line in a file can have max 255 characters.
A line end with new line character.
New line character is converted to carriage
return.
A file end with special character called End of
file (EOF).
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
ASCII Text Files
• There are two representation of data in text
file.
Internal: integer value will be represented as
number that occupies 2 or 4 bytes of memory
internally.
External : integer value will be represented as
Decimal or hexadecimal string of characters.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Binary Files
A binary file contain any type of data , but in a
binary form for storing and processing
purpose.
 a binary file is sequence of bytes.
It is also referred to as character stream.
They are not in human readable form.
All executableprogram are stored in binary
file.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Differences
Binary files requires less space than text files.
Text files are human readable whereas binary
file are not human readable.
Text files are less prone to get corrupted as
compare to binary files.
 Example Text Files : a.txt , a.pdf.
Example Binary file : .png , .jpg
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Opening and Closing Files
• Python has built-in functions to manipulate
files.
The open() Function : to perform any
operation on a file ,file must be opened.
The open() function is used to open a file in
python.
 open() function creates an object , which can
be used to perform different operations on a
file. International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Syntax of open() function
file_obj = open(‘file_name’,mode)
file_obj : Object returned by open() function ,
which is used to perform different operations
on a file. works as a file handle.
file_name : the name of the file that you want
to open.
mode : indicates the mode in which file is to
be opened.example :read, write, append.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Access Modes
Access mode is a optional parameter in open
function.
Default access mode in a open function is read
mode.
A file can be accessed in a read or write mode,
but there are multiple combination of these
modes are avaialble.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Access Modes
Access
Modes
Meaning File pointer
r Default mode ,opens a file in read only mode . at beginning of file.
rb Opens file read only binary format . at beginning of file.
r+ Both reading and writing at beginning of file.
rb + Reading and writing in in binary format at beginning of file.
w
Write only. If file does not exist, new file created. If file
exist ,contents are over written.
at beginning of file.
wb
Opens file in binary format for writing only. If file does
not exist, new file created. If file exist ,contents are
over written.
at beginning of file.
w+
Opens file for both reading and writing. If file does not
exist, new file created. If file exist ,contents are over
written.
at beginning of file.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Basic Operations on Lists
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Access
Modes
Meaning File pointer
wb+
Opens file in binary format for both reading and
writing. If file does not exist, new file created. If file
exist ,contents are over written.
at beginning of file.
a
Opens a file for appending, if file does not exist it
creates a file.
at the end of file.
ab
Opens a file in binary format for appending, if file
does not exist it creates a file.
at the end of file.
a+
Opens a file for reading and appending , if file does
not exist it creates a file.
at the end of file.
ab+
Opens a file in binary format for reading and
appending , if file does not exist it creates a file.
at the end of file.
File Object
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
open() function returns a file object , file object
is used to perform operations on file, which is
also known as file handle.
File handle is different from file , but it used to
access file and perform operations on it.
Even you can print file object
Example : f = open(‘a.txt’,’r’)
Output:< open file ‘a.txt’, mode ‘r’at 0x02A64574>
The File Object Attributes
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
File object can be used to access information
about files.
The information about file can be obtained using
different attributes of a file object.
fileobj.closed :returns true if file is closed ,false
otherwise.
fileobj.mode : returns mode in which file is
opened.
fileobj.name : returns the name of the file.
The Close() Method.
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
The close() method is used to close the file.
Once file object is closed you can not access a
file.
Python automatically closes a file once file
object is reassigned to different file.
close() method closes a file and releases
resources associated with a file.
Example
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
file = open(‘a.txt’,’r’)
file.close()
print(file.name)
print(file.mode)
print(file.closed)
Output :
a.txt
r
True
Reading and Writing Files
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
f= open('a.txt','w')
s= input('Enter a text to write to a file')
f.write(s)
print('the Contents Written to a file a.txt are')
f.close()
f=open('a.txt','r')
print(f.read())
f.close()
Reading and writing files
International Institute of Information
Technology, I²IT, P-14, Rajiv Gandhi
Infotech Park, Hinjawadi Phase 1, Pune -
411 057
Phone - +91 20 22933441/2/3 | Website -
Output:
Enter a text to write to a file:
Welcome to Python
the Contents Written to a file a.txt are
Welcome to Python
References
International Institute of Information Technology, I²IT, P-14,
Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411
057
Phone - +91 20 22933441/2/3 | Website -
www.isquareit.edu.in | Email - info@isquareit.edu.in
 “Python Programming using Problem Solving
Approach” By Reema Thareja ,Oxford University
Pres.
Thank-You
International Institute of Information Technology, I²IT,
P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1,
Pune - 411 057
Phone - +91 20 22933441/2/3 | Website -
www.isquareit.edu.in | Email - info@isquareit.edu.in
International Institute of Information Technology,
I²IT,
P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase
1, Pune - 411 057.
Phone - +91 20 22933441/2/3
Website - www.isquareit.edu.in
Email - info@isquareit.edu.in

More Related Content

PPTX
Functions in python
PDF
Python file handling
PPTX
Error and exception in python
PDF
Python File Handling | File Operations in Python | Learn python programming |...
PDF
PDF
Python libraries
PDF
Python exception handling
PPTX
Variables in python
Functions in python
Python file handling
Error and exception in python
Python File Handling | File Operations in Python | Learn python programming |...
Python libraries
Python exception handling
Variables in python

What's hot (20)

ODP
Python Modules
PPTX
Data file handling in python introduction,opening & closing files
PPTX
Difference Program vs Process vs Thread
PPTX
Packages In Python Tutorial
PPTX
Threads (operating System)
PDF
Python programming : Files
PPSX
Files in c++
PPSX
Modules and packages in python
PPTX
File handling in Python
PPTX
Loops in Python
PPTX
Python Data-Types
PDF
Python programming : Control statements
PPTX
Pointer in c
PPTX
Python Exception Handling
PPTX
Unit I - Evaluation of expression
PDF
Strings in python
PPTX
File Handling Python
PPSX
python Function
PPTX
Static Data Members and Member Functions
Python Modules
Data file handling in python introduction,opening & closing files
Difference Program vs Process vs Thread
Packages In Python Tutorial
Threads (operating System)
Python programming : Files
Files in c++
Modules and packages in python
File handling in Python
Loops in Python
Python Data-Types
Python programming : Control statements
Pointer in c
Python Exception Handling
Unit I - Evaluation of expression
Strings in python
File Handling Python
python Function
Static Data Members and Member Functions
Ad

Similar to File Handling in Python (20)

PDF
23CS101T PSPP python program - UNIT 5.pdf
PPTX
Data file handling in python introduction,opening &amp; closing files
PDF
File handling and Dictionaries in python
PDF
lecs102.pdf kjolholhkl';l;llkklkhjhjbhjjmnj
PDF
File handling in Python this PPT gives
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
PPTX
file_handling_python_bca_computer_python
PDF
File handling4.pdf
PDF
File handling3.pdf
PPTX
File handling for reference class 12.pptx
PPTX
File Handling.pptx
PDF
Python-File handling-slides-pkt
PPTX
for interview this ppt is a teching aid for file handling concepts includes t...
PPTX
Files in Python.pptx
PPTX
Files in Python.pptx
PPTX
File concept and access method
PPTX
What is FIle and explanation of text files.pptx
PPTX
01 file handling for class use class pptx
PPT
File Handling Btech computer science and engineering ppt
PDF
chapter-4-data-file-handlingeng.pdf
23CS101T PSPP python program - UNIT 5.pdf
Data file handling in python introduction,opening &amp; closing files
File handling and Dictionaries in python
lecs102.pdf kjolholhkl';l;llkklkhjhjbhjjmnj
File handling in Python this PPT gives
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
file_handling_python_bca_computer_python
File handling4.pdf
File handling3.pdf
File handling for reference class 12.pptx
File Handling.pptx
Python-File handling-slides-pkt
for interview this ppt is a teching aid for file handling concepts includes t...
Files in Python.pptx
Files in Python.pptx
File concept and access method
What is FIle and explanation of text files.pptx
01 file handling for class use class pptx
File Handling Btech computer science and engineering ppt
chapter-4-data-file-handlingeng.pdf
Ad

More from International Institute of Information Technology (I²IT) (20)

PPTX
Understanding Natural Language Processing
PPTX
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
PPTX
Writing Skills: Importance of Writing Skills
PPTX
Professional Communication | Introducing Oneself
PPTX
PPTX
What Is Jenkins? Features and How It Works
PPTX
Data Science, Big Data, Data Analytics
PPTX
PPTX
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
PPTX
Sentiment Analysis in Machine Learning
PPT
Importance of Theory of Computations
PPT
Java as Object Oriented Programming Language
PPTX
PPTX
Data Visualization - How to connect Microsoft Forms to Power BI
Understanding Natural Language Processing
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Writing Skills: Importance of Writing Skills
Professional Communication | Introducing Oneself
What Is Jenkins? Features and How It Works
Data Science, Big Data, Data Analytics
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Sentiment Analysis in Machine Learning
Importance of Theory of Computations
Java as Object Oriented Programming Language
Data Visualization - How to connect Microsoft Forms to Power BI

Recently uploaded (20)

PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
DevOps & Developer Experience Summer BBQ
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
DevOps & Developer Experience Summer BBQ
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Event Presentation Google Cloud Next Extended 2025
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Transforming Manufacturing operations through Intelligent Integrations
Advanced Soft Computing BINUS July 2025.pdf
Electronic commerce courselecture one. Pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Monthly Chronicles - July 2025
NewMind AI Weekly Chronicles - August'25 Week I
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Smarter Business Operations Powered by IoT Remote Monitoring
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

File Handling in Python

  • 1. File Handling in Python Prof. Anand A Bhosle Department of Information technology International Institute of Information Technology, I²IT www.isquareit.edu.in
  • 2. File • A file is collection of information/data in a particular format. • A file has different extension based on data it stored in it and format it uses for representing that data. • A file is typically stored on a Storage medium such as Disk(hard Disk). • A file is stored on a disk at a location(path). International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 3. File Structure • Files are Stored in a hierarchical structure like a tree. • At top of tree there is a root node. • A root node branches into multiple partitions or drives. • Drives are having folders or directories and files . • Directories in turn may have another folders or files International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 4. Files International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - C:/Myfile (Directory) My Computer C:(Drive) A.txt (Text File) D:( Drive) E: (Drive)
  • 5. File Path • Files can be retrieved or accessed by traversing through the hierarchical( tree) structure as shown in above figure. • So to access a file, we need to traverse from root node to the node of a file. • While traversing , we are visiting the nodes in a sequence to file node is nothing but path or location of a file. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 6. File Path • In figure shown above three drive are root nodes. • A text file is stored on D drive , so we start traversal from D node. • To access a a.txt file , we will traverse from node D to node a.txt . • So the path of the a.txt is : D:Myfilea.txt International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 7. File Path “D:Myfilea.txt “ character after dot indicates the extension of the file. File can have different extension, example .txt, .pdf , .docx , . pptx. In above path character() used to separate the folders or nodes is known as delimiter. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 8. Absolute Path Absolute path is also called complete or full path. Absolute path contains path from root node and all directories and subdirectories that contains a file  D:Myfilea.txt path is the example of absolute path as it contains all nodes from root node to the a.txt node. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 9. Relative Path • A relative path starts with respect to present working directory. • A file can be accessed by using absolute path. • A relative path is combined with another path to form a complete path. Example : C:StudentsMyFolderMyfile.txt If C:Students is present working directory. Then MyFoldeMyfile.txt is Relative Path International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 10. Types of Files Python Supports two types of files. ASCII Text Files Binary Files. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 11. ASCII Text Files In a Text file each character represents a byte. Each character has an ASCII value associated with it. Text files are processed sequentially in forward direction. So , text files can perform one operation at a time(read/write). Text files can read or write one character at a time. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 12. ASCII Text Files A text file can contain zero or more characters. A line in a file can have max 255 characters. A line end with new line character. New line character is converted to carriage return. A file end with special character called End of file (EOF). International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 13. ASCII Text Files • There are two representation of data in text file. Internal: integer value will be represented as number that occupies 2 or 4 bytes of memory internally. External : integer value will be represented as Decimal or hexadecimal string of characters. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 14. Binary Files A binary file contain any type of data , but in a binary form for storing and processing purpose.  a binary file is sequence of bytes. It is also referred to as character stream. They are not in human readable form. All executableprogram are stored in binary file. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 15. Differences Binary files requires less space than text files. Text files are human readable whereas binary file are not human readable. Text files are less prone to get corrupted as compare to binary files.  Example Text Files : a.txt , a.pdf. Example Binary file : .png , .jpg International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 16. Opening and Closing Files • Python has built-in functions to manipulate files. The open() Function : to perform any operation on a file ,file must be opened. The open() function is used to open a file in python.  open() function creates an object , which can be used to perform different operations on a file. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 17. Syntax of open() function file_obj = open(‘file_name’,mode) file_obj : Object returned by open() function , which is used to perform different operations on a file. works as a file handle. file_name : the name of the file that you want to open. mode : indicates the mode in which file is to be opened.example :read, write, append. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 18. Access Modes Access mode is a optional parameter in open function. Default access mode in a open function is read mode. A file can be accessed in a read or write mode, but there are multiple combination of these modes are avaialble. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 19. Access Modes Access Modes Meaning File pointer r Default mode ,opens a file in read only mode . at beginning of file. rb Opens file read only binary format . at beginning of file. r+ Both reading and writing at beginning of file. rb + Reading and writing in in binary format at beginning of file. w Write only. If file does not exist, new file created. If file exist ,contents are over written. at beginning of file. wb Opens file in binary format for writing only. If file does not exist, new file created. If file exist ,contents are over written. at beginning of file. w+ Opens file for both reading and writing. If file does not exist, new file created. If file exist ,contents are over written. at beginning of file. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website -
  • 20. Basic Operations on Lists International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - Access Modes Meaning File pointer wb+ Opens file in binary format for both reading and writing. If file does not exist, new file created. If file exist ,contents are over written. at beginning of file. a Opens a file for appending, if file does not exist it creates a file. at the end of file. ab Opens a file in binary format for appending, if file does not exist it creates a file. at the end of file. a+ Opens a file for reading and appending , if file does not exist it creates a file. at the end of file. ab+ Opens a file in binary format for reading and appending , if file does not exist it creates a file. at the end of file.
  • 21. File Object International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - open() function returns a file object , file object is used to perform operations on file, which is also known as file handle. File handle is different from file , but it used to access file and perform operations on it. Even you can print file object Example : f = open(‘a.txt’,’r’) Output:< open file ‘a.txt’, mode ‘r’at 0x02A64574>
  • 22. The File Object Attributes International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - File object can be used to access information about files. The information about file can be obtained using different attributes of a file object. fileobj.closed :returns true if file is closed ,false otherwise. fileobj.mode : returns mode in which file is opened. fileobj.name : returns the name of the file.
  • 23. The Close() Method. International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - The close() method is used to close the file. Once file object is closed you can not access a file. Python automatically closes a file once file object is reassigned to different file. close() method closes a file and releases resources associated with a file.
  • 24. Example International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - file = open(‘a.txt’,’r’) file.close() print(file.name) print(file.mode) print(file.closed) Output : a.txt r True
  • 25. Reading and Writing Files International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - f= open('a.txt','w') s= input('Enter a text to write to a file') f.write(s) print('the Contents Written to a file a.txt are') f.close() f=open('a.txt','r') print(f.read()) f.close()
  • 26. Reading and writing files International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - Output: Enter a text to write to a file: Welcome to Python the Contents Written to a file a.txt are Welcome to Python
  • 27. References International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected]  “Python Programming using Problem Solving Approach” By Reema Thareja ,Oxford University Pres.
  • 28. Thank-You International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057 Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - [email protected] International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057. Phone - +91 20 22933441/2/3 Website - www.isquareit.edu.in Email - [email protected]