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

PYTHON Implement the recursive function traverse () that takes as input a pathna

ID: 3692600 • Letter: P

Question

PYTHON

Implement the recursive function traverse () that takes as input a pathname of a folder (as a String) and an Integer d and prints on the screen the pathname of every file and subfolder contained in the folder, directly or indirectly. The file and subfolder pathnames should be output with an indentation that proportional to their depth with respect to the topmost folder. The next example illustrates the execution of traverse on the folder ‘test’ shown in below. using a loop (NOT recursion)

>>> traverse(‘test’,0)

test/fileA.txt

test/folder1

                test/folder1/fileB.txt

test/folder1/fileC.txt

test/folder1/folder11

test/folder1/folder11/fileD.txt

test/folder2

                test/folder2/fileD.txt

                test/folder2/file.E.txt

PYTHON only please. I don’t know any other language

Explanation / Answer

Here's a function to do that with formatting: