0% found this document useful (0 votes)
17 views3 pages

Context Managers

The document defines three Python functions: 1) writeTo(filename, input_text) opens filename and writes input_text to the file 2) archive(zfile, filename) zips filename into the zipfile zfile 3) run_process(cmd_args) runs the command line args and returns the output

Uploaded by

Abhilash Jose
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)
17 views3 pages

Context Managers

The document defines three Python functions: 1) writeTo(filename, input_text) opens filename and writes input_text to the file 2) archive(zfile, filename) zips filename into the zipfile zfile 3) run_process(cmd_args) runs the command line args and returns the output

Uploaded by

Abhilash Jose
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/ 3

#!

/bin/python3

import sys
import os
import inspect

# Complete the function below.

def writeTo(filename, input_text):


with open(filename, 'a') as filename:
filename.write(input_text)

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

try:
input_text = str(input())
except:
input_text = 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.')
with open(filename) as fp:
content = fp.read()
if content == input_text:
print('File Contents are :', content)

=======

from zipfile 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, 'a') as filename:
filename.write(input_text)
# Define the function 'archive' below, such that
# it archives 'filename' into the 'zipfile'
def archive(zfile, filename):
with ZipFile(zfile, 'w') as myzip:
myzip.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.')

=======

#!/bin/python3

import sys
import os
import subprocess
import inspect
# Complete the function below.

def run_process(cmd_args):
with subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
as p:
out, err = p.communicate()
return out

if __name__ == "__main__":
f = open(os.environ['OUTPUT_PATH'], 'w')

cmd_args_cnt = 0
cmd_args_cnt = int(input())
cmd_args_i = 0
cmd_args = []
while cmd_args_i < cmd_args_cnt:
try:
cmd_args_item = str(input())
except:
cmd_args_item = None
cmd_args.append(cmd_args_item)
cmd_args_i += 1

res = run_process(cmd_args);
#f.write(res.decode("utf-8") + "\n")

if 'with' in inspect.getsource(run_process):
f.write("'with' used in 'run_process' function definition.\n")

if 'Popen' in inspect.getsource(run_process):
f.write("'Popen' used in 'run_process' function definition.\n")
f.write('Process Output : %s\n' % (res.decode("utf-8")))

f.close()

You might also like