Python Unit 3 notes
Python Unit 3 notes
ZIP is one of the most popular file formats used for archiving and compression. It has been in
use since the days of MSDOS and PC and has been used by famous PKZIP application.
Python's standard library provides zipfile module with classes that facilitate the tools for
creating, extracting, reading and writing to ZIP archives.
ZipFile() function
This function returns a ZipFile object from a file parameter which can be a string or file object as
created by built-in open() function. The function needs a mode parameter whose default value is
'r' although it can take 'w' or 'a' value for opening the archive in read, write or append mode
respectively.
write() method
import zipfile
newzip=zipfile.ZipFile('newzip.zip','w')
newzip.write('zen.txt')
newzip.close()
This creates newzip.zip file in th current directory. Additional file can be added to already
existing archive by opening it in append mode ('a' as the mode).
import zipfile
newzip=zipfile.ZipFile('newzip.zip','a')
newzip.write('json.txt')
newzip.close()
read() method
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
data = newzip.read('json.txt')
print (data)
newzip.close()
Output
printdir() method
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.printdir()
newzip.close()
Output
extract() method
This method extracts a specified file from archive by default to current directory or to one given
as second parameter to it.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.extract('json.txt', 'newdir')
newzip.close()
extractall() method
This method extracts all files in the archive to current directory by default. Specify alternate
directory if required as parameter.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.extractall('newdir')
newzip.close()
getinfo() method
This method returns ZipInfo object corresponding to the given file. The ZipInfo object contains
different metadata information of the file.
Following code obtains ZipInfo object of 'zen.txt' from the archive and retrieves filename, size
and date-time information from it.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
info = newzip.getinfo('zen.txt')
print (info.filename, info.file_size, info.date_time)
newzip.close()
Output
infolist() method
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
info = newzip.infolist()
print (info)
newzip.close()
Output
[<ZipInfo filename='zen.txt' filemode='-rw-rw-rw-' file_size=132>,
<ZipInfo filename='json.txt' filemode='-rw-rw-rw-' file_size=35>]
namelist() method
This method of ZipFile object returns a list of all files in the archive.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
info = newzip.namelist()
print (info)
newzip.close()
Output
['zen.txt', 'json.txt']
setpassword() method
This method sets password parameter which must be provided at the time of extracting the
archive.
The pprint module (lib/pprint.py) is a part of Python’s standard library which is distributed along
with standard Python distribution. The name pprint stands for pretty printer. The pprint module’s
functionality enables aesthetically good looking appearance of Python data structures. Any data
structure that can be correctly parsed by Python interpreter is elegantly formatted. The formatted
expression is kept in one line as far as possible, but breaks into multiple lines if the length
exceeds the width parameter of formatting. One unique feature of pprint output is that the
dictionaries are automatically sorted before the display representation is formatted.
The pprint module contains definition of PrettyPrinter class. Its constructor takes following
format −
The width parameter by default is 80. Desired output is restricted by this value. If the length is
greater than width, it is broken in multiple lines.
The stream parameter is by default std.out – the default output device. It can take any stream
object such as file.
The compact parameter id set to False by default. If true, only the data adjustable within width
will be displayed.
import pprint
students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
pp = pprint.PrettyPrinter()
print ("normal print output")
print (students)
print ("----")
print ("pprint output")
pp.pprint(students)
The pprint module also defines convenience functions pprint() and pformat() corresponding to
PrettyPrinter methods. The example below uses pprint() function.
Next example uses pformat() method as well as pformat() function. To use pformat() method,
PrettyPrinter object is first set up. In both cases, the formatted representation is displayed using
normal print() function.
import pprint
students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
print ("using pformat method")
pp = pprint.PrettyPrinter()
string = pp.pformat(students)
print (string)
print ('------')
print ("using pformat function")
string = pprint.pformat(students)
print (string)
Pretty printer can also be used with custom classes. Inside the class __repr__() method is
overridden. The __repr__() method is called when repr() function is used. It is the official string
representation of Python object. When we use object as parameter to print() function it prints
return value of repr() function.
In following example, the __repr__() method returns the string representation of player object
import pprint
class player:
def __init__(self, name, formats = [], runs = []):
self.name = name
self.formats = formats
self.runs = runs
def __repr__(self):
dct = {}
dct[self.name] = dict(zip(self.formats,self.runs))
return (repr(dct))
l1 = ['Tests','ODI','T20']
l2 = [[140, 45, 39],[15,122,36,67, 100, 49],[78,44, 12, 0, 23, 75]]
p1 = player("virat",l1,l2)
pp = pprint.PrettyPrinter()
pp.pprint(p1)
{'virat': {'Tests': [140, 45, 39], 'ODI': [15, 122, 36, 67, 100, 49], 'T20': [78, 44, 12, 0, 23, 75]}}
If width parameter is changed from default 80 to other value, the output is formatted in such a
way that multiple lines are displayed while care is taken not to violate the syntax.
import pprint
students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
pp=pprint.PrettyPrinter(width = 20)
pp.pprint(students)
The code is similar to first example in this article. However, PrettyPrinter object is constructed
with width parameter as 20. Hence the pprint output is accordingly formatted.
{'Dilip': [ 'English',
'Maths',
'Science'],
'Kalpana': (50,
60,
70),
'Raju': {'English': 50,
'Maths': 60,
'Science': 70}}