0% found this document useful (0 votes)
6 views8 pages

Unit 5.1 File Handling

The document provides an introduction to file handling in Python, detailing file types, operations, and syntax for opening, writing, reading, and closing files. It also covers the OS module for file manipulation and the Pickle module for object serialization and deserialization. Prof. M. V. Tiwari, an expert in Electronics & Telecommunication Engineering, authored the document.

Uploaded by

Ishan Dipte
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)
6 views8 pages

Unit 5.1 File Handling

The document provides an introduction to file handling in Python, detailing file types, operations, and syntax for opening, writing, reading, and closing files. It also covers the OS module for file manipulation and the Pickle module for object serialization and deserialization. Prof. M. V. Tiwari, an expert in Electronics & Telecommunication Engineering, authored the document.

Uploaded by

Ishan Dipte
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/ 8

Subject

Introduction to Python (2SAEKNX01)

Subject Expert
Prof. M. V. Tiwari

B. E. in Electronics & Telecommunication Engineering, H.V.P.M., C.O.E.T., Amravati,


2009.
M. Tech. in Electronic System & Communication, An Autonomous Institute of GCOE,
Amravati, 2013.
Pursuing Ph.D. from P.G. Department of Applied Electronics, S.G.B.A.U.

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


Unit 5 File Handling
File :
➢File is a named location on disk to store related information.
➢It is used to permanently store data in a non – volatile memory (e.g. Hard disk)

File Types :
➢Python can handle two types of files.
I. Text files (.txt)
II. Binary files (Audio, Video, Images, etc.)

File Operations :
➢It takes place in the following order :
I. Open a file
II. Perform file operations (read / write / append)
III.Close the file.

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


Opening a File :

Syntax
f = open(“filename”, “mode”, “buffering”)

open() :- this function opens a file and returns a file object (file handle).

mode :-

w :- write data into file.


➢It creates new file and stores the data.
➢If the file already exists then it would overwrite the data.
r :- read data from file. (default)
➢It reads the data from existing file.
➢If the file does not exists then it gives FileNotFoundError.

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


a:- append data to file.
➢It appends the data to the end of the file.
➢If the file does not exists then it creates new file.

+ in suffix if we want to read and write / append.


b in suffix if we want to work with binary data.

For buffering any positive integer can be used. (default to 4096)

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


File Operations :
Write Operations :
➢write (s) :- write string s to the file and return the number of characters
written.
Note 1 :- string can be text or binary data.
Note 2 :- write method does not add a newline character to the end of the
string.
➢writelines(): - It writes a List of string elements in the file.
Read Operations :
➢read(n) : returns the read bytes in the form of string.
▪ If n is specified then read atmost n characters from the file.
➢readline() : reads and returns one line from the file.
➢readlines(): reads and returns a list of lines from the file.

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


Closing a File:
➢Close( ) will flush out any unwritten information and close the file, after which
no more writing can be done.

➢On closed file any operation would lead to exception: ValueError: I/O operation
on closed file.

➢If an exception occurs when we are performing some operation with file, the code
exits without closing the file.

➢Two operations for safe closure:


▪Use try...finally block or use with statement. (Automatic Resource
Management)

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


OS Module :

➢The os module provides a big range of useful methods to manipulate files and
directories.
▪remove() : it is used to delete file.
▪rename() : it is used for renaming the current file name with new file name.

➢The os.path is a module which provides ways of manipulating paths:


▪isfile() : return True if the file exixts.
▪exists() : returns True if dir / file exists.

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera


Pickle Module :
➢Pickle is an important module when we need persistence between user sessions
(useful for game / forums).
➢It implements an algorithm which turns Python objects into a series of bytes
which can be stored or transmitted (also called serialization / marshalling)
➢Opposite of pickling is unpickling in which object would be constructed from
series of bytes (also called deserialization / unmarshalling).

dump( ) :
➢It takes aserializable Python data structure (list, dictionary etc), serializes it into
binary in an open file.
load( ) :
➢It takes a stram object, reads the serialized data from the stream, creates a new
Python object, recreates the serialized data in the new Python object and returns
the new Python objects.

Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera

You might also like