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.

Python files / directories part15

  • 1.
    Prof. Neeraj Bhargava VishalDutt Department of Computer Science, School of Engineering & System Sciences MDS University, Ajmer
  • 2.
     File handlingis 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 Openinga 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 canspecify 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 ObjectAttributes  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 afile 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 afile 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 isFile Handling in Python?  Explain the important modes for file opening with their work.  Write a python script to open and close the file.