Python QB Solns (Mod 4)
Python QB Solns (Mod 4)
1.
i) copying files and folders
we can copy files and folders by using shutil.copy() method. this method is used to copy the
content of the source file to destination file or directory. the meta data of the file is not
copied in this method.
Syntax: shutil.copy(source, destination, )
where,
source: is the string representing the path of the source file.
destination: A string representing the path of the destination file or directory.
4. explain safe delete. (Other part is permanent delete which is the same.)
Using send2trash, we can send files to the Trash or Recycle Bin instead of permanently
deleting them.
Modules required
OS : The OS module in Python provides functions for interacting with the operating
system. OS module comes with Python’s Standard Library.
send2trash : Send2Trash is a small package that sends files to the Trash (or Recycle Bin)
natively and on all platforms. To install it type the below command in the terminal.
pip install send2trash
import os
import send2trash
for folder, subfolders, files in os.walk('/Users/tithighosh/Documents'):
for file in files:
if file.endswith('.txt'):
path = os.path.join(folder, file)
print('deleted : ', path )
send2trash.send2trash(path)
output:
6.
output
7.
Benefits of compressing files:
1. *Reduced storage space*: Compressing files reduces their size, allowing more data to be
stored in the same amount of space.
2. *Faster file transfer*: Smaller files transfer more quickly over networks or when sending
via email.
3. *Lower bandwidth usage*: Compressed files require less bandwidth when transferring
over the internet, making them faster and cheaper to transmit.
4. *Consolidation*: Multiple files can be compressed into a single archive, making it easier
to manage and share them as a group.
5. *Organization*: Compressed files can help organize related files together, reducing
clutter and simplifying file management.
6. *Security*: Encrypted compression formats can provide an additional layer of security for
sensitive data.
8.
import os
import sys
import pathlib
import zipfile
dirName=input("Enter Directory name that you want to backup")
if not os.path.isdir(dirName):
print("Directory",dirName,"doesnt exists")
sys.exit(0)
curDirectory=pathlib.Path(dirName)
with zipfile.ZipFile("myZip.zip",mode='w')as archive:
for file_path in curDirectory.rglob("*"):
archive.write(file_path,arcname=file_path.relative_to(curDirectory))
if os.path.isfile("myZip.zip"):
print("Archive","myZip.zip","created successfully")
else:
print("Error creating zip archive")
9.
a zipfile is a type of file that contains compressed content of many other files which are
compressed hence, the file size is reduced making it easier to transfer it over the internet.
a single file is known as archive file.
# to create and add to a zip file
>>> import zipfile
>>>new_zip=zipfile.ZipFile(‘new.zip’,’w’)
>>>newZip.write(‘spam.txt’,compress_type=zipfile.ZIP_DEFLATED)
>>>newZip.close()
10. same answer as 8th one
11. illustrate os.walk() function with suitable example
The os.walk() method generates the file and directory names in a directory tree by walking
the tree using top-down or bottom-up approach.
Each directory in the tree is rooted to the top directory. It yields a tuple that contains
directory path, directories name and file name
syntax: os.walk(top)
where,
top represents path like object to be traversed.
15.
output
code