Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What does it do? Given code below, assuming the proper modules are imported, ans

ID: 3861814 • Letter: W

Question

What does it do? Given code below, assuming the proper modules are imported, answer questions after it. def dosomething (param1, param2, param3): os.chdir (param3) for thing in os.listdir (*.*): if os.path.isdir (thing): dosomething (param1, param2, os.path.join (param 3, thing), ) elif os.path.isfile (thing) and thing.endswith(".py"): fp = open(thing, "r") stuff = fp.read () fp.close () if stuff.find("import" + param1) ! = -1: param2.append(os.path.join(param3, thing)) os.chdir("..") if sys.argv [1] [0] = '/': dosomething(sys.argv [2], things, sys.argv [1]) else: dosomething(sys.argv [2], things, os.path.join(os.getcwd(), sys.arg things.sort() for thing in things: print (thing) (a) What do the command line arguments to the script represent? (b) What is this script supposed to do? (c) What do lines 16-19 accomplish with regard to the third parameter of dosomething ()? (d) Why are the chdir () function calls in dosomething () needed? (e) Describe at least one reason why this program could fail to perform its task.

Explanation / Answer

a)

argv[1] is either the directory name or the full path to the directory.

argv[2] is the package name that import in the file.

b)

This script will traverse in all the directories present in the current path and find whether the given import package is present in the python file or not. If the given agrv[2] package is present in the python file then we will add to the list and at atlast return all the files containing the given argument package.

c)

this code checks whether the given argv[1] is path or directory. if it is start from '/' then it is path otherwise add the current path to the directory.

d)

to come out from the current path chdir() function is needed.

e)

if the given directory path could not exist then it will fail.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote