SlideShare a Scribd company logo
Prof. Neeraj Bhargava
Vishal Dutt
Department of Computer Science, School of
Engineering & System Sciences
MDS University, Ajmer
 File handling is an important part of any web
application.
 Python has several functions for creating, reading,
updating, and deleting files.
 The key function for working with files in Python is
the open() function.
 The open() function takes two parameters; filename,
and mode.
File Handling
Modes for Opening a file
 "r" - Read - Default value. Opens a file for reading, error if
the file does not exist
 "a" - Append - Opens a file for appending, creates the file if
it does not exist
 "w" - Write - Opens a file for writing, creates the file if it
does not exist
 "x" - Create - Creates the specified file, returns an error if
the file exists
 you can specify if the file should be handled as binary
or text mode.
 "t" - Text - Default value. Text mode
 "b" - Binary - Binary mode (e.g. images)
Syntax
To open a file for reading it is enough to specify the name of
the file:
f = open("demofile.txt")
//OR
f = open("demofile.txt", "rt")
//here ‘r’ for read and ‘t’ for text are the default values, you do
not need to specify them.
The file Object Attributes
 Once a file is opened and you have one file object, you
can get various information related to that file.
1 file.closed
Returns true if file is closed, false otherwise.
2 file.mode
Returns access mode with which file was opened.
3 file.name
Returns name of the file.
4 file.softspace
Returns false if space explicitly required with print, true
otherwise.
Example
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
Output
Name of the file: foo.txt
Closed or not : False
Opening mode : wb
Softspace flag : 0
The close() Method
 The close() method of a file object flushes any
unwritten information and closes the file object, after
which no more writing can be done.
 Python automatically closes a file when th e reference
object of a file is reassigned to another file. It is a good
practice to use the close() method to close a file.
Syntax:-
fileObject.close()
Example
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
# Close opend file
fo.close()
#Output
Name of the file: foo.txt
Questions
 What is File Handling in Python?
 Explain the important modes for file opening with
their work.
 Write a python script to open and close the file.

More Related Content

What's hot (20)

PDF
Python - Lecture 8
Ravi Kiran Khareedi
 
PPTX
File Handling Python
Akhil Kaushik
 
PPTX
Data file operations in C++ Base
arJun_M
 
PPTX
Files and file objects (in Python)
PranavSB
 
PPT
Mesics lecture files in 'c'
eShikshak
 
PDF
File handling in Python
BMS Institute of Technology and Management
 
PPT
Unit5 C
arnold 7490
 
PPT
File handling(some slides only)
Kamlesh Nishad
 
PPTX
Files in php
sana mateen
 
PPTX
Files
sana mateen
 
PPTX
C Programming Unit-5
Vikram Nandini
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PPTX
File handling
BeebashPokhrel
 
PDF
File operations
PrabhatKumarChaudhar2
 
PPTX
file management in c language
chintan makwana
 
PDF
Python-files
Krishna Nanda
 
PPTX
Data file handling in python introduction,opening & closing files
keeeerty
 
PDF
File handling and Dictionaries in python
nitamhaske
 
PPT
File handling in c
David Livingston J
 
Python - Lecture 8
Ravi Kiran Khareedi
 
File Handling Python
Akhil Kaushik
 
Data file operations in C++ Base
arJun_M
 
Files and file objects (in Python)
PranavSB
 
Mesics lecture files in 'c'
eShikshak
 
Unit5 C
arnold 7490
 
File handling(some slides only)
Kamlesh Nishad
 
Files in php
sana mateen
 
C Programming Unit-5
Vikram Nandini
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
File handling
BeebashPokhrel
 
File operations
PrabhatKumarChaudhar2
 
file management in c language
chintan makwana
 
Python-files
Krishna Nanda
 
Data file handling in python introduction,opening & closing files
keeeerty
 
File handling and Dictionaries in python
nitamhaske
 
File handling in c
David Livingston J
 

Similar to Python files / directories part15 (20)

PDF
23CS101T PSPP python program - UNIT 5.pdf
RajeshThanikachalam
 
PPTX
Python File Handling52616416416 ppt.pptx
saiwww3841k
 
PPTX
pspp-rsk.pptx
ARYAN552812
 
PPTX
File handling in Python
Megha V
 
PPT
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
PPTX
Unit-VI.pptx
Mehul Desai
 
PPTX
files.pptx
KeerthanaM738437
 
PPTX
File Handling in C
VrushaliSolanke
 
PPT
Python file handlings
22261A1201ABDULMUQTA
 
PPTX
Data File Handling in Python Programming
gurjeetjuneja
 
PPTX
FIle Handling and dictionaries.pptx
Ashwini Raut
 
PDF
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
PDF
Python Files I_O17.pdf
RashmiAngane1
 
PPTX
INput output stream in ccP Full Detail.pptx
AssadLeo1
 
PPTX
Python files / directories part16
Vishal Dutt
 
PPTX
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
PPTX
File handling in C hhsjsjshsjjsjsjs.pptx
armaansohail9356
 
PPTX
Chapter - 5.pptx
MikialeTesfamariam
 
PPTX
Filesin c++
HalaiHansaika
 
23CS101T PSPP python program - UNIT 5.pdf
RajeshThanikachalam
 
Python File Handling52616416416 ppt.pptx
saiwww3841k
 
pspp-rsk.pptx
ARYAN552812
 
File handling in Python
Megha V
 
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
Unit-VI.pptx
Mehul Desai
 
files.pptx
KeerthanaM738437
 
File Handling in C
VrushaliSolanke
 
Python file handlings
22261A1201ABDULMUQTA
 
Data File Handling in Python Programming
gurjeetjuneja
 
FIle Handling and dictionaries.pptx
Ashwini Raut
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
Python Files I_O17.pdf
RashmiAngane1
 
INput output stream in ccP Full Detail.pptx
AssadLeo1
 
Python files / directories part16
Vishal Dutt
 
this is about file concepts in class 12 in python , text file, binary file, c...
Primary2Primary2
 
File handling in C hhsjsjshsjjsjsjs.pptx
armaansohail9356
 
Chapter - 5.pptx
MikialeTesfamariam
 
Filesin c++
HalaiHansaika
 
Ad

More from Vishal Dutt (20)

PPTX
Grid computing components
Vishal Dutt
 
PPTX
Python Classes and Objects part14
Vishal Dutt
 
PPTX
Python Classes and Objects part13
Vishal Dutt
 
PPTX
Python functions part12
Vishal Dutt
 
PPTX
Python functions part11
Vishal Dutt
 
PPTX
Python functions part10
Vishal Dutt
 
PPTX
List view5
Vishal Dutt
 
PPTX
Python decision making_loops_control statements part9
Vishal Dutt
 
PPTX
List view4
Vishal Dutt
 
PPTX
List view3
Vishal Dutt
 
PPTX
Python decision making_loops_control statements part8
Vishal Dutt
 
PPTX
Python decision making_loops part7
Vishal Dutt
 
PPTX
Python decision making_loops part6
Vishal Dutt
 
PPTX
List view2
Vishal Dutt
 
PPTX
List view1
Vishal Dutt
 
PPTX
Python decision making part5
Vishal Dutt
 
PPTX
Python decision making part4
Vishal Dutt
 
PPTX
Python operators part3
Vishal Dutt
 
PPTX
Python operators part2
Vishal Dutt
 
PPTX
Python part1
Vishal Dutt
 
Grid computing components
Vishal Dutt
 
Python Classes and Objects part14
Vishal Dutt
 
Python Classes and Objects part13
Vishal Dutt
 
Python functions part12
Vishal Dutt
 
Python functions part11
Vishal Dutt
 
Python functions part10
Vishal Dutt
 
List view5
Vishal Dutt
 
Python decision making_loops_control statements part9
Vishal Dutt
 
List view4
Vishal Dutt
 
List view3
Vishal Dutt
 
Python decision making_loops_control statements part8
Vishal Dutt
 
Python decision making_loops part7
Vishal Dutt
 
Python decision making_loops part6
Vishal Dutt
 
List view2
Vishal Dutt
 
List view1
Vishal Dutt
 
Python decision making part5
Vishal Dutt
 
Python decision making part4
Vishal Dutt
 
Python operators part3
Vishal Dutt
 
Python operators part2
Vishal Dutt
 
Python part1
Vishal Dutt
 
Ad

Recently uploaded (20)

PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 

Python files / directories part15

  • 1. Prof. Neeraj Bhargava Vishal Dutt Department of Computer Science, School of Engineering & System Sciences MDS University, Ajmer
  • 2.  File handling is an important part of any web application.  Python has several functions for creating, reading, updating, and deleting files.  The key function for working with files in Python is the open() function.  The open() function takes two parameters; filename, and mode. File Handling
  • 3. Modes for Opening a file  "r" - Read - Default value. Opens a file for reading, error if the file does not exist  "a" - Append - Opens a file for appending, creates the file if it does not exist  "w" - Write - Opens a file for writing, creates the file if it does not exist  "x" - Create - Creates the specified file, returns an error if the file exists
  • 4.  you can specify if the file should be handled as binary or text mode.  "t" - Text - Default value. Text mode  "b" - Binary - Binary mode (e.g. images) Syntax To open a file for reading it is enough to specify the name of the file: f = open("demofile.txt") //OR f = open("demofile.txt", "rt") //here ‘r’ for read and ‘t’ for text are the default values, you do not need to specify them.
  • 5. The file Object Attributes  Once a file is opened and you have one file object, you can get various information related to that file. 1 file.closed Returns true if file is closed, false otherwise. 2 file.mode Returns access mode with which file was opened. 3 file.name Returns name of the file. 4 file.softspace Returns false if space explicitly required with print, true otherwise.
  • 6. Example # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name print "Closed or not : ", fo.closed print "Opening mode : ", fo.mode print "Softspace flag : ", fo.softspace Output Name of the file: foo.txt Closed or not : False Opening mode : wb Softspace flag : 0
  • 7. The close() Method  The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.  Python automatically closes a file when th e reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file. Syntax:- fileObject.close()
  • 8. Example # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Close opend file fo.close() #Output Name of the file: foo.txt
  • 9. Questions  What is File Handling in Python?  Explain the important modes for file opening with their work.  Write a python script to open and close the file.