Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python Unit 3 notes

Uploaded by

Sneha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Unit 3 notes

Uploaded by

Sneha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Compressing Files with the zipfile Module

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.

The archive by default is uncompressed. To specify the type of compression algorithm to be


used, one of the constants has to be assigned to compression parameter.

zipfile.ZIP_STORED for an uncompressed archive member.

for the usual ZIP compression method. This


zipfile.ZIP_DEFLATED
requires the zlib module.

for the BZIP2 compression method. This requires


zipfile.ZIP_BZIP2
the bz2 module.

for the LZMA compression method. This requires


zipfile.ZIP_LZMA
the lzma module.

The ZipFile object uses following methods −

write() method

This method adds the given file to the ZipFile object.

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

This method reads data from a particular file in the archive.

import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
data = newzip.read('json.txt')
print (data)
newzip.close()

Output

b'["Rakesh", {"marks": [50, 60, 70]}]'

printdir() method

This method lists all file in given archive.

import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.printdir()
newzip.close()

Output

File Name Modified Size


zen.txt 2023-03-30 21:55:48 132
json.txt 2023-04-03 22:01:56 35

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

zen.txt 132 (2023, 3, 30, 21, 55, 48)

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.

pprint module (Data pretty printer)

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 −

pprint.PrettyPrinter(indent, width, depth, stream, compact)


The indent parameter defines indentation added on each recursive level. Default is 1.

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 depth parameter controls number of levels to be printed.

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.

The PrettyPrinter class defines following methods −

pprint() − prints the formatted representation of PrettyPrinter object

pformat() − Returns the formatted representation of object, based on parameters to the


constructor.

Following example demonstrates simple use of PrettyPrinter class.

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 output shows normal as well as pretty print display.

normal print output


{'Dilip': ['English', 'Maths', 'Science'], 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}, 'Kalpana':
(50, 60, 70)}
----
pprint output
{'Dilip': ['English', 'Maths', 'Science'],
'Kalpana': (50, 60, 70),
'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}

The pprint module also defines convenience functions pprint() and pformat() corresponding to
PrettyPrinter methods. The example below uses pprint() function.

from pprint import pprint


students = {"Dilip":["English", "Maths", "Science"],
"Raju":{"English":50,"Maths":60, "Science":70},
"Kalpana":(50,60,70)}
print ("normal print output")
print (students)
print ("----")
print ("pprint output")
pprint (students)

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)

Here is the output of above code

using pformat method


{'Dilip': ['English', 'Maths', 'Science'],
'Kalpana': (50, 60, 70),
'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}
------
using pformat function
{'Dilip': ['English', 'Maths', 'Science'],
'Kalpana': (50, 60, 70),
'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}

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)

The output of above code is −

{'virat': {'Tests': [140, 45, 39], 'ODI': [15, 122, 36, 67, 100, 49], 'T20': [78, 44, 12, 0, 23, 75]}}

Recursive data structure with pprint


When we try to print a recursive object with pprint, only first representation is displayed and for
subsequent recursions, only its reference is printed.

>>> import pprint


>>> numbers = list(range(1,6))
>>> numbers.append(numbers)
>>> print (numbers)
[1, 2, 3, 4, 5, [...]]
>>> pprint.pprint(numbers)
[1, 2, 3, 4, 5, <Recursion on list with id=1403633698824>]

Restricting output width

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}}

You might also like