0% found this document useful (0 votes)
7 views68 pages

File Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views68 pages

File Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

FILE HANDLING

Prepared by: Ankit Prabhakar


NEED FOR A DATA FILE

• To Store data in organized manner


• To store data permanently
• To access data faster
• To Search data faster
• To easily modify data later on
File Handling

It refers to the process of creating ,opening, reading ,writing ,


appending and closing files using programming techniques to store
and manage data permanently on a storage device .

File handling in Python enables us to create, update, read, and delete


the files stored on the file system through our python program. The
following operations can be performed on a file.
File Handling

In Python, File Handling consists of following three steps:


1. Open the file.
2. Process file i.e. perform read or write operation.
3. Close the file.
TYPES OF FILE

Text
file

CSV
file
Binary
File
TEXT FILE
Text Files-

• A file whose contents can be viewed using a text editor is called a text file.

• A text file is simply a sequence of ASCII or Unicode characters.

• In text files , Each line of text is terminated (delimited ) with a special character

know as EOL character (End of Line ) character.

• In python , by default , this EOL character is the new line character (‘\n’) or

carriage return .
TYPES OF FILE
• Text Files can be of following types :
(i) Regular text files – These are text files which store the text in the same form
as typed . Here the new line character ends a line and the text translation take
place . These files have a file extension.txt
(ii) Delimited Text files- In these text files , a specific character is stored to
separate the values , i.e. , after each value e.g. a tab or a comma after every
value.
1. When a tab character is used to separate the values stored , these are called
TSV files ( Tab Separate Values Files). These files can take the extension as .txt
or.csv.
2. When the comma is used to separate the values stored , these are called CSV
files (Comma Separate Values files). These files take the extension as .csv.
TYPES OF FILE continued ….
CSV Files- The CSV( Comma separated Values) format is a popular import
and export format for spreadsheet database. Most commonly used delimiter in
a CSV file is comma(,),but it can also use other delimiter characters like tab( )
, pipe (|), tilde (~) etc.

Binary Files- A binary file stores the information in the form of a stream of
bytes . A binary file contains information in the same format in which the
information is held in the memory. In binary file , there is no delimiter for a line.
Also no translation occur in binary files. As a result , binary files are faster and
easier for a program to read and write than are text files. The .exe files,mp3 file,
image files, word documents are some of the examples of binary files. we can’t
read a binary file using a text editor.e.g. .bmp,.cdr etc.
Basic file operations

 Open (filename – absolute or relative path, mode)

 Reading/Writing data

 Manipulation of data

 Appending data into a text file

 Close a file
Opening and Closing Files
 To perform file operation ,it must be opened first then after reading , writing, editing
operation can be performed.
 To create any new file then too it must be opened.
 On opening of any file ,a file relevant structure is created in memory as well as
memory space is created to store contents.
 Once we are done working with the file, we should close the file.
 Closing a file releases valuable system resources. In case we forgot to close the file,
Python automatically close the file when program ends or file object is no longer
referenced in the program. However, if our program is large and we are reading or
writing multiple files that can take significant amount of resource on the system.
 If we keep opening new files carelessly, we could run out of resources. So be a good
programmer , close the file as soon as all task are done with it.
Opening Files
 Before any reading or writing operation of any file, it must be

opened first of all. Python provide built in function open() for it.

On calling of this function creates file object for file operations.

 Syntax

<file-objectname> = open(<filename>)

<file-objectname> = open(<filename>,<mode>)

For example:- myfile=open(“taxes.txt”)

NOTE: file Object is also known as File handle


Opening Files

2
File Access Mode
Text File Binary File Description Notes
Mode Mode

‘r’ ‘rb’ Read only DEFAULT MODE ; File must exist


already , otherwise Python raise I/O
error.

‘w’ ‘wb’ Write only • If the File doesn't exist , File is created.
• If the file exist, Python will truncate
existing
data and over write in the file. SO this
mode
must be used with caution.
File Access Mode
Text File Binary File Description Notes
Mode Mode
‘a’ ‘ab’ Append • File is in write only mode.
• If the file exist the data in the file is
retained
and new data being written will be
appended to
the end.
• If the File doesn't exist , Python will
create a
new file .
‘r+’ ‘r+b’ or ‘rb+’ Read and Write • File must exist otherwise error is
raised.
• Both reading and writing operations
can take
place.
File Access Mode
Text File Binary File Description Notes
Mode Mode
‘w+’ ‘w+b’ or Write and • File is created if does not exist.
‘wb+’ read • If the file exist , File is truncated . (past
data is lost)
• Both reading and writing operations can
take
place.
‘a+’ ‘a+b’ or ‘ab+’ Write and • File is created if does not exist.
read • If the file exist, file’s existing data is
reatined ; new
data is appended.
• Both reading and writing operations can
take
place.
Closing a file
An open file is closed by calling the close () method of its file-object .
Closing of file is important .
In python , Files are automatically closed at the end of the program.
The close() function accomplish this task and it takes the following
general form:
<filehandle>.close()
For Instance , if a file master.txt is opened via file handle outfile, it may
be closed by the following statement:

outfile.close()
TEXT FILES TOPICS
 Opening a text file

 Opening a file using with clause

 Text file open modes (r, r+, w, w+, a, a+)

 Closing a text file

 Writing/appending data to a text file using write() and writelines()

 Reading from a text file using read()

 Readline() and readlines()

 Seek and Tell methods

 Manipulation of data in a text file


Opening a text file
 To open a file in Python just you have to use open () function as per following
syntax:
<file-object name>=open (<file name>)
<file-object name>=open (<file name>,<mode>)
Opening a file using with clause
 When we are using with clause you do not have to close the file it will
automatically closes as soon as you came out of with block.
File Access Mode in text File
Text File Mode Description Notes

‘r’ Read only DEFAULT MODE ; File must exist already , otherwise
Python raise I/O error.

‘w’ Write only • If the File doesn't exist , File is created.


• If the file exist, Python will truncate existing
data and over write in the file. SO this mode
must be used with caution.
‘a’ Append • File is in write only mode.
• If the file exist the data in the file is retained
and new data being written will be appended to
the end.
• If the File doesn't exist , Python will create a
new file .
‘r+’ Read and Write • File must exist otherwise error is raised.
• Both reading and writing operations can take
place.
File Access Mode in text File

Text File Mode Description Notes

‘w+’ Write and read • File is created if does not exist.


• If the file exist , File is truncated . (past data is lost)
• Both reading and writing operations can take
place.

‘a+’ Write and read • File is created if does not exist.


• If the file exist, file’s existing data is retained ; new
data is appended.
• Both reading and writing operations can take
place.
Closing a text file

The file is closed.


Writing data to a text file using write() and writelines()
 We can write characters into file by using following two methods -

1. write (string)

2. writelines (sequence of lines)

 write ( ): It takes a sting as argument and adds to the file. We have to use ‘\n’

in string for end of line character.

 writelines ( ) : If we want to write list, tuple into the file then we use writelines ( ) function.
Writing data to a text file using write()
Writing data to a text file using write()
Writing data to a text file using writelines()
Writing data to a text file using writelines()
Writing data to a text file using writelines()
Appending in a Text File
 Append means adding something new to existing file.
 ‘a’ mode is used to accomplish this task. It means opening a file in write
mode and if file is existing then adding data to the end of the file.
Writing User Input to the File.
Writing User Input to the File.
Reading from a text file using read()
S.No Method Syntax Description

1. read() <filehandle>.read([n]) • reads at most n bytes ; if no n is specified , reads the


entire file.
2. readline() <filehandle>.readline([n • reads a line of input ; if n is specified reads at most n
]) bytes.
• Returns the read bytes in the form of a string ending
with In(line) character or returns a blank string if no
more bytes are left for reading in the file.
• Reads only a single line at a time.
3. readlines() <filehandle>.readlines() • reads all the lines and returns them in a list
Reading from a text file using read()
Example 1
Example 2
readline() and readlines()
 We can also use readline( ) function which can read one line at a time from the file.
 Same readlines( ) function is used to read many lines.
readline() and readlines()
readline() and readlines()
readline() and readlines()
seek () and tell () Function
 The tell() method of python tells us the current position within the file.
 It is used as per syntax:
<file-object>.tell()
 The seek(offset[,from]) method changes the current file position . If from is 0, the
beginning of the file to seek. If it is set to 1, the current position is used. If it is set
to 2 then the end of the file would be taken as seek position. The offset argument
indicates the number of bytes to be moved.
 It is used as per syntax:
<file-object>.seek(offset[,from])
seek () and tell () Function
BINARY FILES TOPICS
 Open using file open modes (rb, rb+, wb, wb+, ab, ab+)

 Close a binary file

 import pickle module

 Dump() and Load() method

 Read

 Write/Create

 Search

 Append

 Update operations in a binary file


Binary File
 A binary file is a file stored in binary format.

 A binary file is computer-readable but not human readable.

 All executable programs are stored in binary files, as most numeric data files.

 Sometimes you may need to write and read non- simple objects like

dictionaries,tuples ,lists or nested lists and so forth on to the files.


Binary File

For this purpose , objects are often serialized and then stored in binary files.

 Serialization (also called Pickling) is the process of converting Python


object hierarchy into a byte stream so that it can be written into a file.
Pickling converts an object in byte stream in such a way that it can be
reconstructed in original form when unpickled

 Unpickling is the inverse of Pickling where a byte stream is converted


into an object hierarchy. Unpickling produces the exact replica of the
original object.
Binary File
 In order to work with pickle module , we must import it the program using import
statement:
 import pickle

 And then we may use dump() and load() methods of pickle module to write and read from
an open binary file respectively.

 Process of working with binary files is similar to the process we have done so far just a
little difference that we work with pickle module in binary files, i.e.

(i) Import pickle module

(ii) Open binary file in the required file mode (read or write mode)

(iii) Process binary file by writing /reading objects using pickle module's methods.

(iv) Once done ,close the file.


File Access Modes
Binary File Mode Description Notes
‘rb’ Read only DEFAULT MODE ; File must exist already , otherwise Python
raise I/O error.

‘wb’ Write only • If the File doesn't exist , File is created.


• If the file exist, Python will truncate existing
data and over write in the file. SO this mode
must be used with caution.

‘ab’ Append • File is in write only mode.


• If the file exist the data in the file is retained
and new data being written will be appended to
the end.
• If the File doesn't exist , Python will create a
new file .

‘r+b’ or ‘rb+’ Read and Write • File must exist otherwise error is raised.
• Both reading and writing operations can take
place.
File Access Modes
Binary File Mode Description Notes

‘w+b’ or ‘wb+’ Write and read • File is created if does not exist.
• If the file exist , File is truncated . (past data is lost)
• Both reading and writing operations can take
place.
‘a+b’ or ‘ab+’ Write and read • File is created if does not exist.
• If the file exist, file’s existing data is reatined ; new
data is appended.
• Both reading and writing operations can take
place.
Creating/Opening and Closed Function
 Open () Function is used to open a binary file same as text file.
 The only difference is in the mode in which it is opened.

Eg. f= open (“hello.dat”,”rb”)


 Close () function is also same as text file.

f.close()
Writing onto a binary File-Pickling
 In order to write an object on to a binary file opened in the write mode, we

should use dump() function of pickle module as per following syntax:

pickle.dump(<object-to-be-written>,<file –handle –of-open-file>)

For instance , if we have a open a file open in handle file1 and we have to

write a list namely list1 in the file, then we have to write :

pickle.dump(list1,file1)

In the same way, you may write dictionaries, tuples or any other Python object in

binary file using dump() function.


Reading from a binary file-Unpickling
 To read from the file using load() function of the pickle module as it would then

unpickle the data coming from the file.

 The load() function is used as per following syntax:

<object>=pickle.load(<filehandle>)

For instance , to read an object nemp from a file open in file –handle fout , you

would write:

nemp=pickle.load(fout)
IMPORTANT
 But before we move further onto the program code , it is important to know

that pickle.load() function would raise EOF Error( a run time Exception)

when you reach end –of-file while reading from the file.

 We can handle this by following one of the below given two methods.

 Use try and except blocks

 Using with statement


Use try and except blocks

 Thus , we must write pickle.load() enclosed in try and except statements

given in the program later. The try and except statements together , can

handle runtime exceptions.

 In the try block ,i.e. between the try and except keywords , you write the

code that can generate an exception and in the except block ,i.e. below the

except keyword , you write what to do when exception occurs.

(EOF-end of file in this case)


Use try and except blocks

<file-handle>=open(<filename>,<readmode>) In the try block, write the pickle.load()


try: statement and other processing
<object>=pickle.load(<file-handle>) statements. In order to read all the
# other processing statements records , read inside a loop is shown.

except EOFERROR: Use this keyword with except keyword for checking EOF (end of file)

In the except block , write code for what to do when EOF


exception has occurred

<file-handle>.close()
Using with statement

 The with statement is a compact statement which combines the opening of file

and processing of file along with inbuilt exception handling .

 The with statement will also close the file automatically after with block is over .

 We can use with statement as :

with open (<filename>, <mode>) as <file handle>:

# use pickle.load here in this with block

# perform other file manipulation task in this with block


Search the Data in Binary File After Storing
Search the Data in Binary File After Storing
Search the Data in Binary File After Storing
Read the Data after Storing in Binary File
CSV FILE (Comma separated value)

 CSV(Comma Separated Values) is a file format for data

storage which looks like a text file. The information is

organized with one record on each line and each field is

separated by comma.
CSV File Characteristics

 One line for each record

 Comma separated fields

 Space-characters adjacent to commas are ignored

 Fields with in-built commas are separated by double quote characters.


When Use CSV?

 When data has a strict tabular structure

 To transfer large database between programs

 To import and export data to office applications.

 To store, manage and modify shopping cart catalogue.


CSV
 To work with csv files we need to import csv module.
 Syntax: import csv
 Functions of csv module :
1. csv.reader ():
syntax : csv.reader(file-name)
2. csv.writer () :
syntax :csv.writer(file-name)
Writing in CSV Files

csv.writer() Returns a writer object which writes data into csv file

<writerobject>.writerow() Writes one row of data onto the writer object

<writerobject>.writerows() Writes multiple rows of data onto the writer object.


Writing in CSV Files

csv writer object


With the help of converts the user Csv file on
User data Delimited data
csv.writerow() data into csv storage disk
writable form
Writing into a CSV file
Writing into a CSV file
Read in CSV File

You might also like