Nov 19: Recursive Walk, Redo Quiz5
Learning Objectives
After today's class, you should be able to:
- Explain how the organization of a file system is recursive.
- Describe a recursive function that traverses a file system.
File System Review¶
[5 min]
- Folders contain files and other folders, which contain files and other folders, which …
- An everyday example of recursion!
- Two kinds of paths
- An "absolute" path (from root of the file system) begins with a slash
- A "relative" path (from the current folder) does not begin with a slash
Example
- Absolute path to
CS149folder:/home/mayfiecs/CS149(begins with a slash) - Relative path to selected file:
Homework/HW5/scores.py(does not begin with a slash) - Absolute path to selected file:
/home/mayfiecs/CS149/Homework/HW5/scores.py

Using os and os.path¶
[5 min]
os – Miscellaneous operating system interfaces
os.getcwd()– get current working directoryos.listdir()– get list of directory entries
os.path – Common pathname manipulations
os.path.isfile(path)– True if regular fileos.path.basename(path)– get name of the fileos.path.join(path, path2)– combine two paths
Example: print_files¶
[15 min]
A recursive function that "walks" the file system: filesys1.py
Sample output when run on my PA1 folder:
PA1:
.pytest_cache:
.gitignore
CACHEDIR.TAG
README.md
v:
cache:
lastfailed
nodeids
__pycache__:
analysis.cpython-312.pyc
simplify.cpython-312.pyc
test_analysis.cpython-312-pytest-8.4.2.pyc
test_simplify.cpython-312-pytest-8.4.2.pyc
analysis.py
main.py
simplify.py
test_analysis.py
test_simplify.py
Quiz 5 Coding Redo¶
[20 min]
- Log in as
student - Start the recording
- Log in to Gradescope
- You must use Thonny (not VS Code)
- Do not turn off recording; just log out