file handling in python using exception statement
 A file (i.e. data file) is a named place on the disk
where a sequence of related data is stored. In
python files are simply stream of data, so the
structure of data is not stored in the file, along
with data.
 Basic operations performed on a data file are:
 Naming a file
 Opening a file
 Reading data from the file
 Writing data in the file
 Closing a file
 Using these basic operations, we can process file
in many ways, such as
 Creating a file
 Traversing a file for displaying the data on screen
 Appending data in file
 Inserting data in file
 Deleting data from file
 Create a copy of file
 Updating data in the file, etc.
 Python allow us to create and manage two types
of file
 Text
 Binary
 A text file is usually considered as sequence
of lines. Line is a sequence of characters
(ASCII), stored on permanent storage media.
Although default character coding in python
is ASCII but using constant u with string,
supports Unicode as well.
 Each line is terminated by a special character,
known as End of Line (EOL). From strings we
know that n is newline character. So at the
lowest level, text file will be collection of
bytes. Text files are stored in human readable
form and they can also be created using any
text editor
 A binary file contains arbitrary binary data i.e.
numbers stored in the file, can be used for
numerical operation(s). So when we work on
binary file, we have to interpret the raw bit
pattern(s) read from the file into correct type of
data in our program.
 It is perfectly possible to interpret a stream of
bytes originally written as string, as numeric
value. But we know that will be incorrect
interpretation of data and we are not going to get
desired output after the file processing activity.
 So in the case of binary file it is extremely
important that we interpret the correct data type
while reading the file. Python provides special
module(s) for encoding and decoding of data for
binary file.
 To handle data files in python, we need to have
a file object. Object can be created by using
open() function or file() function. To work on
file, first thing we do is open it. This is done by
using built in function open().
 Using this function a file object is created
which is then used for accessing various
methods and functions available for file
manipulation.
 Syntax of open() function is
 file_object = open(filename [, access_mode]
[,buffering])
 open() requires three arguments to
work, first one ( filename ) is the
name of the file . The name can
include the description of path.
 The second parameter (access_mode)
describes how file will be used
throughout the program. This is an
optional parameter and the default
access_mode is reading.
 The third parameter (buffering) is for specifying
how much is read from the file in one read. The
function will return an object of file type using
which we will manipulate the file, in our program.
When we work with file(s), a buffer (area in
memory where data is temporarily stored before
being written to file), is automatically associated
with file when we open the file. While writing the
content in the file, first it goes to buffer and once
the buffer is full, data is written to the file. Also
when file is closed, any unsaved data is
transferred to file. flush() function is used to
force transfer of data from buffer to file.
 File access modes
 r will open the text file for reading only and rb will do
the same for binary format file.
 w will open a text file for writing only and wb for
binary format file.
 r+ will open a text file and rb+ will open a binary file,
for both reading and writing purpose. The file pointer
is placed at the beginning of the file when it is
opened using r+ / rb+ mode.
 w+ opens a file in text format and wb+ in binary
format, for both writing and reading. File pointer will
be placed at the beginning for writing into it, so an
existing file will be overwritten. A new file can also be
 created using this mode.
 a+ opens a text file and ab+ opens a binary file, for
both appending and reading. File pointer is placed at
 the end of the file, in an already existing file. Using
this mode a non existing file may be created.
 fileobject. close() will be used to close the file
object, once we have finished working on it.
 readline() will return a line read, as a string
from the file
 fileobject.readline()
 Since the method returns a string it's usage
will be
 >>>x = file.readline()
 or
 >>>print file.readline()
 readlines()can be used to read the entire
content of the file. You need to be careful
while using it w.r.t. size of memory required
before using the function. The method will
return a list of strings, each separated by
 n.
 An example of reading entire data of file in
list is:
 It's syntax is:
 fileobject.readlines()
 as it returns a list, which can then be used for
manipulation.
 read() can be used to read specific size string
from file. This function also returns a string read
from the file.
 At the end of the file, again an empty string will
be returned.
 Syntax of read() function is
 fileobject.read([size])
 Here size specifies the number of bytes to be
read from the file.
 lines = []
 content = file.read() # since no size is given,
entire file will be read
 lines = content.splitlines()
 print lines
 will give you a list of strings:
 ['hello world.', 'this is my first file handling
program.', 'I am using python language.']
 For sending data in file, i.e. to create / write
in the file, write() and writelines() methods
can be used.
 write() method takes a string ( as parameter )
and writes it in the file. For storing data with
end of line character, you will have to add n
character to end of the string.
 Notice addition of n in the end of every
sentence while talking of data.txt. As
argument to the function has to be string, for
storing numeric value, we have to convert it
to string.
 Its syntax is
 fileobject.write(string)
 Example
 >>>f = open('test1.txt','w')
 >>>f.write("hello worldn")
 >>>f.close()
 For numeric data value conversion to string is
required.
 Example
 >>>x = 52
 >>>file.write(str(x))
 For writing a string at a time, we use write()
method, it can't be used for writing a list, tuple
etc. into a file.
 Sequence data type can be written using
writelines() method in the file. It's not that, we
can't write a string
 using writelines() method.
 It's syntax is:
 fileobject.writelines(seq)
 So, whenever we have to write a sequence of
string / data type, we will use writelines(), instead
of write().
 f = open('test2.txt','w')
 str = 'hello world.n this is my first file handling
program.n I am using python language"
 f.writelines(str)
 f.close()
 To access the contents of file randomly - seek and tell
methods are used.
 tell() method returns an integer giving the current position
of object in the file. The integer returned specifies the
number of bytes from the beginning of the file till the
current position of file object.
It's syntax is
 fileobject.tell()
 seek()method can be used to position the file object at
particular place in the file. It's syntax is :
 fileobject.seek(offset [, from_what])
 here offset is used to calculate the position of fileobject in
the file in bytes. Offset is added to from_what (reference
point) to get the position. Following is the list of
from_what values:
 Value reference point
 0 beginning of the file
 1 current position of file
 2 end of file
 default value of from_what is 0, i.e. beginning of the file.
 So for storing data in binary format, we will use
pickle module.
 First we need to import the module. It provides
two main methods for the purpose, dump and
load. For
 creation of binary file we will use pickle.dump()
to write the object in file, which is opened in
binary access mode.
 Syntax of dump() method is:
 dump(object, fileobject)
 Example:
 def fileOperation1():
 import pickle
 l = [1,2,3,4,5,6]
 file = open('list.dat', 'wb') # b in access mode is
for binary file
 pickle.dump(l,file) # writing content to binary file
 file.close()
file handling in python using exception statement
file handling in python using exception statement
file handling in python using exception statement
file handling in python using exception statement
 Files are always stored in current folder / directory by
default. The os (Operating System) module ofpython
provides various methods to work with file and folder
/ directories. For using these functions, we have to
import os module in our program.
 Some useful methods, which can be used with files in
os module are as follows:
 path.abspath(filename) will give us the complete path
name of the data file.
 path.isfile(filename) will check, whether the file exists
or not.
 remove(filename) will delete the file. Here filename
has to be the complete path of file.
 rename(filename1,filename2) will change the name of
filename1 with filename2.
 Once the file is opened, then using file object,
we can derive various information about file.
This is done using file attributes defined in os
module. Some of the attributes defined in it
are
 1. file.closed returns True if file is closed
 2. file.mode returns the mode, with which file
was opened.
 3. file.name returns name of the file
associated with file object while opening the
file.
 os.chdir(path) Change the current working
directory to path.
 os.getcwd() Return a string representing the
current working directory.
 os.chmod(path, mode) Change the mode of
path to the numeric mode. mode may take
one of the following values (as defined in the
stat module) or bitwise ORed combinations of
them:
 os.listdir(path) Return a list containing the
names of the entries in the directory given by
path. The list is in arbitrary order. It does not
include the special entries '.' and '..' even if
they are present in the directory.
 s.mkdir(path[, mode]) Create a directory named path
with numeric mode mode. The default mode is 0777
(octal). On some systems, mode is ignored. Where it
is used, the current umask value is first masked out.
If the directory already exists, OSError is raised.
 os.rmdir(path) Remove (delete) the directory path.
Only works when the directory is empty, otherwise,
OSError is raised. In order to remove whole directory
trees, shutil.rmtree() can be used.
 os.rename(src, dst) Rename the file or directory src to
dst. If dst is a directory, OSError will be raised. stems.
If successful, the renaming will be an atomic
operation (this is a POSIX requirement).
 On Windows, if dst already exists, OSError will be
raised even if it is a file; there may be no way to
implement an atomic rename when dst names an
existing file.
file handling in python using exception statement
 def fileHandling():
 file = open("story.txt","w+") # both reading &
writing
 choice = True
 while True:
 line = raw_input("enter sentence :")
 file.write(line) # creation of file
 choice = raw_input("want to enter more data in
file Y / N")
 if choice == 'N' : break
 file.seek(0) # transferring file object to beginning
of the file
 lines = file.readlines()
 file.close()
 for l in lines:
 print l

More Related Content

PPTX
FILE HANDLING.pptx
PPTX
DFH PDF-converted.pptx
PPTX
Data File Handling in Python Programming
PPTX
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
PPTX
Chapter 08 data file handling
PPTX
Data file handling in python reading & writing methods
PPTX
Data file handling in python reading & writing methods
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
FILE HANDLING.pptx
DFH PDF-converted.pptx
Data File Handling in Python Programming
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
Chapter 08 data file handling
Data file handling in python reading & writing methods
Data file handling in python reading & writing methods
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge

Similar to file handling in python using exception statement (20)

PPTX
File Handling
PPTX
FILE INPUT OUTPUT.pptx
PPTX
Module 5_Reading and Writing Files.pptx.
PPTX
FILE HANDLING IN PYTHON Presentation Computer Science
PPTX
FILE HANDLING in python to understand basic operations.
PPTX
working with files
PPTX
file handling.pptx avlothaan pa thambi popa
PDF
FILES IN C
DOCX
File Handling in python.docx
PDF
Data file handling
PPTX
Files.pptx
PPTX
FIle Handling and dictionaries.pptx
PPTX
Python-FileHandling.pptx
PPTX
File handling for reference class 12.pptx
PPT
7 Data File Handling
PDF
Filepointers1 1215104829397318-9
PPTX
VKS-Python-FIe Handling text CSV Binary.pptx
PPTX
Programming C- File Handling , File Operation
PDF
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
File Handling
FILE INPUT OUTPUT.pptx
Module 5_Reading and Writing Files.pptx.
FILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING in python to understand basic operations.
working with files
file handling.pptx avlothaan pa thambi popa
FILES IN C
File Handling in python.docx
Data file handling
Files.pptx
FIle Handling and dictionaries.pptx
Python-FileHandling.pptx
File handling for reference class 12.pptx
7 Data File Handling
Filepointers1 1215104829397318-9
VKS-Python-FIe Handling text CSV Binary.pptx
Programming C- File Handling , File Operation
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
Ad

Recently uploaded (20)

PPTX
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
PPTX
MICROPARA INTRODUCTION XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
PDF
My India Quiz Book_20210205121199924.pdf
PDF
plant tissues class 6-7 mcqs chatgpt.pdf
PDF
M.Tech in Aerospace Engineering | BIT Mesra
PPTX
Module on health assessment of CHN. pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
English Textual Question & Ans (12th Class).pdf
PDF
Everyday Spelling and Grammar by Kathi Wyldeck
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
Farming Based Livelihood Systems English Notes
PDF
Civil Department's presentation Your score increases as you pick a category
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
MICROPARA INTRODUCTION XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
My India Quiz Book_20210205121199924.pdf
plant tissues class 6-7 mcqs chatgpt.pdf
M.Tech in Aerospace Engineering | BIT Mesra
Module on health assessment of CHN. pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
International_Financial_Reporting_Standa.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
English Textual Question & Ans (12th Class).pdf
Everyday Spelling and Grammar by Kathi Wyldeck
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
AI-driven educational solutions for real-life interventions in the Philippine...
Farming Based Livelihood Systems English Notes
Civil Department's presentation Your score increases as you pick a category
Ad

file handling in python using exception statement

  • 2.  A file (i.e. data file) is a named place on the disk where a sequence of related data is stored. In python files are simply stream of data, so the structure of data is not stored in the file, along with data.  Basic operations performed on a data file are:  Naming a file  Opening a file  Reading data from the file  Writing data in the file  Closing a file
  • 3.  Using these basic operations, we can process file in many ways, such as  Creating a file  Traversing a file for displaying the data on screen  Appending data in file  Inserting data in file  Deleting data from file  Create a copy of file  Updating data in the file, etc.  Python allow us to create and manage two types of file  Text  Binary
  • 4.  A text file is usually considered as sequence of lines. Line is a sequence of characters (ASCII), stored on permanent storage media. Although default character coding in python is ASCII but using constant u with string, supports Unicode as well.  Each line is terminated by a special character, known as End of Line (EOL). From strings we know that n is newline character. So at the lowest level, text file will be collection of bytes. Text files are stored in human readable form and they can also be created using any text editor
  • 5.  A binary file contains arbitrary binary data i.e. numbers stored in the file, can be used for numerical operation(s). So when we work on binary file, we have to interpret the raw bit pattern(s) read from the file into correct type of data in our program.  It is perfectly possible to interpret a stream of bytes originally written as string, as numeric value. But we know that will be incorrect interpretation of data and we are not going to get desired output after the file processing activity.  So in the case of binary file it is extremely important that we interpret the correct data type while reading the file. Python provides special module(s) for encoding and decoding of data for binary file.
  • 6.  To handle data files in python, we need to have a file object. Object can be created by using open() function or file() function. To work on file, first thing we do is open it. This is done by using built in function open().  Using this function a file object is created which is then used for accessing various methods and functions available for file manipulation.  Syntax of open() function is  file_object = open(filename [, access_mode] [,buffering])
  • 7.  open() requires three arguments to work, first one ( filename ) is the name of the file . The name can include the description of path.  The second parameter (access_mode) describes how file will be used throughout the program. This is an optional parameter and the default access_mode is reading.
  • 8.  The third parameter (buffering) is for specifying how much is read from the file in one read. The function will return an object of file type using which we will manipulate the file, in our program. When we work with file(s), a buffer (area in memory where data is temporarily stored before being written to file), is automatically associated with file when we open the file. While writing the content in the file, first it goes to buffer and once the buffer is full, data is written to the file. Also when file is closed, any unsaved data is transferred to file. flush() function is used to force transfer of data from buffer to file.
  • 9.  File access modes  r will open the text file for reading only and rb will do the same for binary format file.  w will open a text file for writing only and wb for binary format file.  r+ will open a text file and rb+ will open a binary file, for both reading and writing purpose. The file pointer is placed at the beginning of the file when it is opened using r+ / rb+ mode.  w+ opens a file in text format and wb+ in binary format, for both writing and reading. File pointer will be placed at the beginning for writing into it, so an existing file will be overwritten. A new file can also be  created using this mode.  a+ opens a text file and ab+ opens a binary file, for both appending and reading. File pointer is placed at  the end of the file, in an already existing file. Using this mode a non existing file may be created.
  • 10.  fileobject. close() will be used to close the file object, once we have finished working on it.  readline() will return a line read, as a string from the file  fileobject.readline()  Since the method returns a string it's usage will be  >>>x = file.readline()  or  >>>print file.readline()
  • 11.  readlines()can be used to read the entire content of the file. You need to be careful while using it w.r.t. size of memory required before using the function. The method will return a list of strings, each separated by  n.  An example of reading entire data of file in list is:  It's syntax is:  fileobject.readlines()  as it returns a list, which can then be used for manipulation.
  • 12.  read() can be used to read specific size string from file. This function also returns a string read from the file.  At the end of the file, again an empty string will be returned.  Syntax of read() function is  fileobject.read([size])  Here size specifies the number of bytes to be read from the file.  lines = []  content = file.read() # since no size is given, entire file will be read  lines = content.splitlines()  print lines  will give you a list of strings:  ['hello world.', 'this is my first file handling program.', 'I am using python language.']
  • 13.  For sending data in file, i.e. to create / write in the file, write() and writelines() methods can be used.  write() method takes a string ( as parameter ) and writes it in the file. For storing data with end of line character, you will have to add n character to end of the string.  Notice addition of n in the end of every sentence while talking of data.txt. As argument to the function has to be string, for storing numeric value, we have to convert it to string.
  • 14.  Its syntax is  fileobject.write(string)  Example  >>>f = open('test1.txt','w')  >>>f.write("hello worldn")  >>>f.close()  For numeric data value conversion to string is required.  Example  >>>x = 52  >>>file.write(str(x))
  • 15.  For writing a string at a time, we use write() method, it can't be used for writing a list, tuple etc. into a file.  Sequence data type can be written using writelines() method in the file. It's not that, we can't write a string  using writelines() method.  It's syntax is:  fileobject.writelines(seq)  So, whenever we have to write a sequence of string / data type, we will use writelines(), instead of write().  f = open('test2.txt','w')  str = 'hello world.n this is my first file handling program.n I am using python language"  f.writelines(str)  f.close()
  • 16.  To access the contents of file randomly - seek and tell methods are used.  tell() method returns an integer giving the current position of object in the file. The integer returned specifies the number of bytes from the beginning of the file till the current position of file object. It's syntax is  fileobject.tell()  seek()method can be used to position the file object at particular place in the file. It's syntax is :  fileobject.seek(offset [, from_what])  here offset is used to calculate the position of fileobject in the file in bytes. Offset is added to from_what (reference point) to get the position. Following is the list of from_what values:  Value reference point  0 beginning of the file  1 current position of file  2 end of file  default value of from_what is 0, i.e. beginning of the file.
  • 17.  So for storing data in binary format, we will use pickle module.  First we need to import the module. It provides two main methods for the purpose, dump and load. For  creation of binary file we will use pickle.dump() to write the object in file, which is opened in binary access mode.  Syntax of dump() method is:  dump(object, fileobject)  Example:  def fileOperation1():  import pickle  l = [1,2,3,4,5,6]  file = open('list.dat', 'wb') # b in access mode is for binary file  pickle.dump(l,file) # writing content to binary file  file.close()
  • 22.  Files are always stored in current folder / directory by default. The os (Operating System) module ofpython provides various methods to work with file and folder / directories. For using these functions, we have to import os module in our program.  Some useful methods, which can be used with files in os module are as follows:  path.abspath(filename) will give us the complete path name of the data file.  path.isfile(filename) will check, whether the file exists or not.  remove(filename) will delete the file. Here filename has to be the complete path of file.  rename(filename1,filename2) will change the name of filename1 with filename2.
  • 23.  Once the file is opened, then using file object, we can derive various information about file. This is done using file attributes defined in os module. Some of the attributes defined in it are  1. file.closed returns True if file is closed  2. file.mode returns the mode, with which file was opened.  3. file.name returns name of the file associated with file object while opening the file.
  • 24.  os.chdir(path) Change the current working directory to path.  os.getcwd() Return a string representing the current working directory.  os.chmod(path, mode) Change the mode of path to the numeric mode. mode may take one of the following values (as defined in the stat module) or bitwise ORed combinations of them:  os.listdir(path) Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
  • 25.  s.mkdir(path[, mode]) Create a directory named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If the directory already exists, OSError is raised.  os.rmdir(path) Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised. In order to remove whole directory trees, shutil.rmtree() can be used.  os.rename(src, dst) Rename the file or directory src to dst. If dst is a directory, OSError will be raised. stems. If successful, the renaming will be an atomic operation (this is a POSIX requirement).  On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.
  • 27.  def fileHandling():  file = open("story.txt","w+") # both reading & writing  choice = True  while True:  line = raw_input("enter sentence :")  file.write(line) # creation of file  choice = raw_input("want to enter more data in file Y / N")  if choice == 'N' : break  file.seek(0) # transferring file object to beginning of the file  lines = file.readlines()  file.close()  for l in lines:  print l