/
print_dirs.py
26 lines (19 loc) · 902 Bytes
/
print_dirs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""A sample program showing how to recurse on directories using os."""
import os
def print_items(d: str, indentation: str) -> None:
"""A sample program showing how to recurse on directories using os.
Print the list of files and directories in directory <d>, recursively,
prefixing each with the given indentation.
"""
print(indentation + d + ':')
for filename in os.listdir(d):
print(indentation + filename)
subitem = os.path.join(d, filename)
if os.path.isdir(subitem):
print_items(subitem, indentation + ' ')
if __name__ == '__main__':
# Put in a path like
# 'C:\\Users\\David\\Documents\\csc148\\assignments' (Windows) or
# '/Users/dianeh/Documents/courses/csc148/assignments' (OSX)
# to print the contents of that folder.
print_items('/Users/jessica/OneDrive - University of Toronto/csc148/assignments', '')