PYTHON Implement the recursive function traverse ( String,int ) that takes as in
ID: 3692825 • Letter: P
Question
PYTHON
Implement the recursive function traverse (String,int) 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.
>>> 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
Side note** I asked this question twice already and two Anonymous persond answered the question wrong. one didn't even answer the question they pasted from a different question found on Chegg. it was related to the question but didn't answer the question.
Explanation / Answer
def traverse(path,num):
return_list=[ ]
for filenames in os.walk(path):
for files in filenames :
for file_name in files:
if file_name.endswitch((".txt")):
return_list.append(file_name)
return return_list
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.