SlideShare a Scribd company logo
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

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

Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
ShaswatSurya
 
file handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popafile handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
Python file handling
Python file handlingPython file handling
Python file handling
Prof. Dr. K. Adisesha
 
FILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer ScienceFILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
file handling.pdf
file handling.pdffile handling.pdf
file handling.pdf
RonitVaskar2
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
22261A1201ABDULMUQTA
 
FILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptxFILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptx
ssuserd0df33
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
MikialeTesfamariam
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
Python data file handling
Python data file handlingPython data file handling
Python data file handling
ToniyaP1
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
RashmiAngane1
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
Python File functions
Python File functionsPython File functions
Python File functions
keerthanakommera1
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.pptx
MasterDarsh
 
File Handling Btech computer science and engineering ppt
File Handling Btech computer science and engineering pptFile Handling Btech computer science and engineering ppt
File Handling Btech computer science and engineering ppt
pinuadarsh04
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Akhil Kaushik
 
file handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popafile handling.pptx avlothaan pa thambi popa
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.File Operations in python Read ,Write,binary file etc.
File Operations in python Read ,Write,binary file etc.
deepalishinkar1
 
5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx5-filehandling-2004054567151830 (1).pptx
5-filehandling-2004054567151830 (1).pptx
lionsconvent1234
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta
 
FILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer ScienceFILE HANDLING IN PYTHON Presentation Computer Science
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
FILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptxFILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptx
ssuserd0df33
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
Python data file handling
Python data file handlingPython data file handling
Python data file handling
ToniyaP1
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
RashmiAngane1
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.pptx
MasterDarsh
 
File Handling Btech computer science and engineering ppt
File Handling Btech computer science and engineering pptFile Handling Btech computer science and engineering ppt
File Handling Btech computer science and engineering ppt
pinuadarsh04
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Akhil Kaushik
 

Recently uploaded (20)

Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
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