Mod 4 Solutions
Mod 4 Solutions
MODULE -4
1. Explain permanent delete and safe delete with a suitable Python programming
example to each. (8M)
25
>>> baconFile.close()
>>> send2trash.send2trash('bacon.txt')
Ex:
x = -10
assert x > 0, "x should be positive"
print("Assertion passed!")
Output :AssertionError: x should be positive
What it does:
3. Explain the functions with examples: (i) shutil.copytree() (ii) shutil.move() (iii)
shutil.rmtree(). (6M)
i. shutil.copytree()
The shutil module provides functions for copying files, as well as entire folders. Calling
shutil.copy(source, destination) will copy the file at the path source to the folder at the
path destination
The shutil.copytree() call creates a new folder named bacon_backup with the same
content as the original bacon folder
For Example
>>> import shutil, os
>>> os.chdir('C:\\')
>>> shutil.copytree('C:\\bacon', 'C:\\bacon_backup')
'C: \\bacon_backup'
ii. shutil.move()
Calling shutil.move(source, destination) will move the file or folder at the path source
to the path destination and will return a string of the absolute path of the new location.
>>> import shutil
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
'C:\\eggs\\bacon.txt'
➢ Assuming a folder named eggs already exists in the C:\ directory, this shutil.move()
calls says, “Move C:\bacon.txt into the folder C:\eggs.”.
iii. shutil.rmtree().
Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it
contains will also be deleted.
Ex:
import shutil
import os
os.mkdir("my_folder")
shutil.rmtree("my_folder")
print("Folder deleted successfully!")
5. List out the benefits of compressing file?Also explain reading of a zip file with an
example(8M)
Compressing a file reduces its size, which is useful when transferring it over the
Internet.
This single file, called an archive file, can then be, say, attached to an email
Reduces file sizes, freeing up valuable disk or cloud storage.Ueful for archiving large
files like documents, media, or backups.
Smaller files take less time to upload, download, or send over networks.Ideal for
sharing files via email, cloud, or messaging apps.
To read the contents of a ZIP file, first you must create a ZipFile object (note the
capital letters Z and F).
ZipFile objects are conceptually similar to the File objects you saw returned by the
open() function They are values through which the program interacts with the file.
To create a ZipFile object, call the zipfile.ZipFile() function
Ex:
>>> import zipfile, os
>>> os.chdir('C:\\') # move to the folder with example.zip
>>> exampleZip = zipfile.ZipFile('example.zip')
>>> exampleZip.namelist()
['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
Functionality Copies a single file. Copies an entire folder and its contents.
import shutil
import shutil
# Copying a file (spam.txt) to a folder
# Copying an entire folder (bacon) to a
Example Usage (delicious)
new folder (bacon_backup)
shutil.copy('spam.txt', 'delicious')
shutil.copytree('bacon', 'bacon_backup')
Ex:
x = -10
assert x > 0, "x should be positive"
print("Assertion passed!")
Output :AssertionError: x should be positive
raise a exception
➢ For example
ERROR logging.error() Used to record an error that caused the program to fail in some way.
The logging.disable() function disables these so that you don’t have to go into your program
and remove all the logging calls by hand.
pass logging.disable() a logging level, and it will suppress all log messages at that level or
lower
>>> import logging
>>> logging.basicConfig(level=logging.INFO, format=' %(asctime)s -%(levelname)s -
%(message)s') >>> logging.critical('Critical error! Critical error!')
2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!
>>> logging.disable(logging.CRITICAL)