0% found this document useful (0 votes)
4K views7 pages

Module 4 Important Questions With Answers

Chemistry vtu

Uploaded by

Aniket Redekar
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)
4K views7 pages

Module 4 Important Questions With Answers

Chemistry vtu

Uploaded by

Aniket Redekar
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/ 7

Module-4

Organizing Files & Debugging

1. Explain permanent delete and safe delete with a suitable Python


programming example to each.

2. Explain the role of Assertions in Python with a suitable program.

3. Explain the functions with examples: (i) shutil.copytree() (ii) shutil.move()


(iii) shutil.rmtree()

4. Explain the support for Logging with logging module in Python.

5. Explain the following file operations in python with suitable examples.


(i) Copying files and folders
(ii) Moving files and folders

6. List out the difference between shutil.copy() & shutil.copytree()

7. What is the use of ZIP? How to create a ZIP folder explain.

8. Explain the logging levels.

Programs
1. Develop a program to backing up a given Folder (Folder in a current
working directory) into a ZIP File by using relevant modules and
suitable methods.
1. Explain permanent delete and safe delete with a suitable Python
programming example to each.
Permanent delete
You can delete a single file or a single empty folder with functions in the
os module, whereas to delete a folder and all of its contents, you use the
shutil module.
• Calling os.unlink(path) will delete the file at path.
• Calling os.rmdir(path) will delete the folder at path. This folder must
be empty of any files or folders.
• Calling shutil.rmtree(path) will remove the folder at path, and all files
and folders it contains will also be deleted.
import os
from pathlib import Path
for filename in Path.home().glob('*.rxt'):
os.unlink(filename)

Safe delete
Since Python’s built-in shutil.rmtree() function irreversibly deletes files
and folders, it can be dangerous to use. A much better way to delete files
and folders is with the third-party send2trash module
Using send2trash is much safer than Python’s regular delete functions,
because it will send folders and files to your computer’s trash or recycle
bin instead of permanently deleting them.
>>> import send2trash
>>> baconFile = open('bacon.txt', 'a') # creates the file
>>> baconFile.write('Bacon is not a vegetable.')
25
>>> baconFile.close()
>>> send2trash.send2trash('bacon.txt')
2. Explain the role of Assertions in Python with a suitable program.
An assertion is a sanity check to make sure your code isn’t doing something
obviously wrong. These sanity checks are performed by assert statements. If
the sanity check fails, then an AssertionError exception is raised.
>>> ages = [26, 57, 92, 54, 22, 15, 17, 80, 47, 73]
>>> ages.sort()
>>> ages
[15, 17, 22, 26, 47, 54, 57, 73, 80, 92]
>>> assert ages[0] <= ages[-1] # Assert that the first age is <= the last age.

The assert statement here asserts that the first item in ages should be less than or equal to
the last one. This is a sanity check; code in sort() is bug-free and did its job, then the assertion
would be true.

>>> ages = [26, 57, 92, 54, 22, 15, 17, 80, 47, 73]
>>> ages.reverse()
>>> ages
[73, 47, 80, 17, 15, 22, 54, 92, 57, 26]
>>> assert ages[0] <= ages[-1] # Assert that the first age is <= the last age.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError

3. Explain the functions with examples: (i) shutil.copytree() (ii)


shutil.move() (iii) shutil.rmtree()
(i) shutil.copytree():
shutil.copytree() will copy an entire folder and every folder and file
contained in it. Calling shutil.copytree(source, destination) will copy the
folder at the path source, along with all of its files and subfolders, to the
folder at the path destination. The source and destination parameters are
both strings. The function returns a string of the path of the copied folder.
>>> import shutil, os
>>> os.chdir('C:\\')
>>> shutil.copytree('C:\\bacon', 'C:\\bacon_backup')
'C:\\bacon_backup'
The shutil.copytree() call creates a new folder named bacon_backup with
the same content as the original bacon folder. You have now safely backed
up your precious, precious bacon
(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. If destination points to a
folder, the source file gets moved into destination and keeps its
current filename. For example
>>> import shutil
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
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.”'C:\\eggs\\bacon.txt'

(iii) shutil.rmtree():
Since Python’s built-in shutil.rmtree() function irreversibly deletes
files and folders, it can be dangerous to use. The shutil. rmtree(path)
function is used to remove a directory and all of its contents,
including all subdirectories and files.
Calling shutil.rmtree(path) will remove the folder at path, and all
files and folders it contains will also be deleted.

4. Explain the support for Logging with logging module in Python.


Python’s logging module makes it easy to create a record of custom
messages that you write. These log messages will describe when the
program execution has reached the logging function call and list any
variables you have specified at that point in time. On the other hand, a
missing log message indicates a part of the code was skipped and never
executed.
Using the logging Module
To enable the logging module to display log messages on your screen as
your program runs, copy the following to the top of your program (but
under the #! python shebang line):
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s -
%(levelname)s - %(message)s')
You don’t need to worry too much about how this works, but basically,
when Python logs an event, it creates a LogRecord object that holds
information about that event.
5. Explain the following file operations in python with suitable examples.
(i) Copying files and folders
(ii) Moving files and folders

(i) Copying files and folders


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. (Both
source and destination are strings.) If destination is a filename, it
will be used as the new name of the copied file.
>>> import shutil, os
>>> os.chdir('C:\\')
>>> shutil.copy('C:\\spam.txt', 'C:\\delicious')
'C:\\delicious\\spam.txt'
>>> shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt')
'C:\\delicious\\eggs2.txt'

(ii) Moving files and folders


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. If destination points to a
folder, the source file gets moved into destination and keeps its
current filename. For example
>>> import shutil
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
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.”'C:\\eggs\\bacon.txt'

6. List out the difference between shutil.copy() & 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. (Both source and
destination are strings.) If destination is a filename, it will be used as the
new name of the copied file. This function returns a string of the path of
the copied file:
>>> import shutil, os
>>> os.chdir('C:\\')
>>> shutil.copy('C:\\spam.txt', 'C:\\delicious')
'C:\\delicious\\spam.txt'
>>> shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt')
'C:\\delicious\\eggs2.txt'
The first shutil.copy() call copies the file at C:\spam.txt to the folder
C:\delicious. The return value is the path of the newly copied file.
shutil.copytree():
While shutil.copy() will copy a single file, shutil.copytree() will copy an
entire folder and every folder and file contained in it. Calling
shutil.copytree(source, destination) will copy the folder at the path source,
along with all of its files and subfolders, to the folder at the path destination
>>> import shutil, os
>>> os.chdir('C:\\')
>>> shutil.copytree('C:\\bacon', 'C:\\bacon_backup')
'C:\\bacon_backup'
The shutil.copytree() call creates a new folder named bacon_backup with
the same content as the original bacon folder. You have now safely backed
up your precious, precious bacon

7. What is the use of ZIP? How to create a ZIP folder explain.


To create your own compressed ZIP files, you must open the ZipFile object
in write mode by passing 'w' as the second argument. (This is similar to
opening a text file in write mode by passing 'w' to the open() function.)
When you pass a path to the write() method of a ZipFile object, Python
will compress the file at that path and add it into the ZIP file.
The write() method’s first argument is a string of the filename to add. The
second argument is the compression type parameter, which tells the
computer what algorithm it should use to compress the files; you can
always just set this value to zipfile.ZIP_DEFLATED.
>>> import zipfile
>>> newZip = zipfile.ZipFile('new.zip', 'w')
>>> newZip.write('spam.txt',
compress_type=zipfile.ZIP_DEFLATED)
>>> newZip.close()
8. Explain the logging levels.
Logging levels provide a way to categorize your log messages by
importance. There are five logging levels, described in Table 10-1 from
least to most important. Messages can be logged at each level using a
different logging function.

You might also like