Home / Programs / Archiving a file using 'with' - Python Context Manager - Hands On
🚀 Programming Example

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

👁 536 Views
💻 Practical Program
📘 Step Learning

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

📌 Information & Algorithm

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 Code

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.')    
    


                        

🖥 Program 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.
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.