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: 3692598 • 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.

>>> 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

Ans;

Here’s a complete but small example module:

If you run example.py directly from the command line, doctest works its magic:

There’s no output! That’s normal, and it means all the examples worked. Pass -v to the script, and doctest prints a detailed log of what it’s trying, and prints a summary at the end:

And so on, eventually ending with:

That’s all you need to know to start making productive use of doctest! Jump in. The following sections provide full details. Note that there are many examples of doctests in the standard Python test suite and libraries. Especially useful examples can be found in the standard test file Lib/test/test_doctest.py.


**************************

the following code writes a Unicode string into a file, encoding it as UTF-8:


******************************

Until now string-manipulation functionality was in the string module, which was usually a front-end for the strop module written in C. The addition of Unicode posed a difficulty for the strop module, because the functions would all need to be rewritten in order to accept either 8-bit or Unicode strings. For functions such as string.replace(), which takes 3 string arguments, that means eight possible permutations, and correspondingly complicated code.

Instead, Python 2.0 pushes the problem onto the string type, making string manipulation functionality available through methods on both 8-bit strings and Unicode strings.

>>>

>>> 'andrew'.capitalize()

'Andrew'

>>> 'hostname'.replace('os', 'linux')

'hlinuxtname'

>>> 'moshe'.find('sh')

2

One thing that hasn’t changed, a noteworthy April Fools’ joke notwithstanding, is that Python strings are immutable. Thus, the string methods return new strings, and do not modify the string on which they operate.

The old string module is still around for backwards compatibility, but it mostly acts as a front-end to the new string methods.

Two methods which have no parallel in pre-2.0 versions, although they did exist in JPython for quite some time, are startswith() and endswith(). s.startswith(t) is equivalent to s[:len(t)] == t, while s.endswith(t) is equivalent to s[-len(t):] == t.

One other method which deserves special mention is join(). The join() method of a string receives one parameter, a sequence of strings, and is equivalent to the string.join() function from the old string module, with the arguments reversed. In other words, s.join(seq) is equivalent to the old string.join(seq, s).