Archiving a file using 'with' - Python Context Manager - Hands On

Python Python Classes and Objects (Article) Python Classes and Objects (Program)

446

2. Archiving a file using 'with'

• Define a function 'archive' with two parameters, 'zfile' and 'filename'. 'zfile' refers to the *.zip' file to which the file 'filename' is archived.

• Define a function 'writeTo' with two parameters, 'filename' and 'text'. 'text' takes any input string, and 'filename' refers to the name of the file to which the input text is written.

Hint: Use 'zipfile' module and 'with'.

Given Input:

sample.txt
python is awesome
myarchive.zip

Expected Output:

'with' used in 'writeTo' function definition.
File : sample.txt is present on system.
'with' used in 'archive' function definition.
ZipFile : myarchive.zip is present on system.

Program:

import zipfile
import sys
import os
import inspect

# Define 'writeTo' function below, such that 
# it writes input_text string to filename.

def writeTo(filename, input_text):
    with open(filename, "w") as f:
        f.write(input_text)

    

# Define the function 'archive' below, such that
# it archives 'filename' into the 'zipfile'
def archive(zfile, filename):
    with zipfile.ZipFile(zfile, "w") as zip:
        zip.write(filename)
    

if __name__ == "__main__":
    try:
        filename = str(input())
    except:
        filename = None

    try:
        input_text = str(input())
    except:
        input_text = None

        
    try:
        zip_file = str(input())
    except:
        zip_file = None
        
    res = writeTo(filename, input_text)
    
    if 'with' in inspect.getsource(writeTo):
        print("'with' used in 'writeTo' function definition.")
        
    if os.path.exists(filename):
        print('File :',filename, 'is present on system.')
 
    res = archive(zip_file, filename)
    
    if 'with' in inspect.getsource(archive):
        print("'with' used in 'archive' function definition.")
        
    if os.path.exists(zip_file):
        print('ZipFile :',zip_file, 'is present on system.')    
    

Output:

'with' used in 'writeTo' function definition.
File : sample.txt is present on system.
'with' used in 'archive' function definition.
ZipFile : myarchive.zip is present on system.

This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.