Announcements CompSci 101 • Reading and RQ 17 due next time Introduction to Computer Science • Assignment 7 due today – Assign 8 due April 21, Assign 9 due April 28 – APT 10 due Tuesday

April 14, 2016 • Today: Prof. Rodger – How do you access directories – Recursion – Solving problems by solving smaller and smaller similar problems – Exam back compsci101 spring16 1 compsci101 spring16 2

Plan for Today What's in a file-system Folder? • Programming and understanding … – Hierarchical structures and concepts • What is a on a computer? • What is the Internet? • How does the Domain Name System Work? • How do you access directories? • And all the files in a , and the …

compsci101 spring16 3 compsci101 spring16 4 What's in a folder on your computer? Finding large files: FileVisit.py • Where are the large files? How do you def bigfiles(dirname,min_size): large = [] find them? They take up space! Erase? for sub in os.listdir(dirname): path = os.path.join(dirname,sub) Backup? if os.path.isdir(path): – Can a folder be inside a folder? Why? subs = bigfiles(path,min_size) large.extend(subs) else: size = os.path.getsize(path) if size > min_size: large.append((path,size)) return large

# on Mac like this: #bigs = bigfiles("/Users/Susan/Documents",10000) # on Windows like this: compsci101 spring16 5 bigs = bigfiles("C:\\Users\\Susan\\Documents",10000)

Example Run Does the function call itself? No!

• ('C:\\Users\\Susan\\files\\courses\\cps101\\w def visit(dirname): orkspace\\spring2015\\assign4_transform\\d for inner in dirname: if isdir(inner): ata\\romeo.txt', 153088L) visit(inner) • ('C:\\Users\\Susan\\files\\courses\\cps101\\w else: orkspace\\spring2015\\assign4_transform\\d print name(inner), size(inner) ata\\twain.txt', 13421L) • Is a file inside itself? No! • ('C:\\Users\\Susan\\files\\courses\\cps101\\w • Does pseudo code make sense? orkspace\\spring2015\\assign5_hangman\\sr – Details make this a little harder in Python, c\\lowerwords.txt', 408679L) but close!

•… compsci101 spring16 7 compsci101 spring16 8 Structure matches Code Recursion

• If you see a folder • Structure matches code, or concept matches – Find large files, if you find a folder, … code – To compress folder, must compress … – Must have a base case when no recursive call • Files and repeat process for folders made • Simpler/smaller calls • Structure of list of lists – Can also lead to processing a list which requires processing a list which … • The case of Quicksort [ [ [a,b], [c,d], [a, [b,c],d] ] – Inventor didn't get it (a *(b + c (d + e*f)) + (a* (b+d))) compsci101 spring16 9 compsci101 spring16 10

Sir Anthony (Tony) Hoare The os and os.path libraries

There are two ways of • Use an API to isolate system dependencies constructing a software – C:\\x\\y # windows design. One way is to make – /Users/Susan/Desktop # mac it so simple that there are obviously no deficiencies. And the other way is to • FAT-32, ReFS, WinFS, HFS, HSF+, fs make it so complicated that – Underneath, these systems are different there are no obvious Turing Award, didn’t get – Python API insulates and protects programmer deficiencies. recursion…..

• Why do we have os.path.join(x,y)? Inventor of quicksort compsci101 spring16 11 compsci101 spring16 12 os. And os.path questions Dissecting FileVisit.py bit.ly/101sp16-0414-1 • How do we find the contents of a folder? – Another name for folder: directory

• How do we identify folder? (by name) – os.listdir(dirname) returns a list of files and folder

• Path is c:\user\ola\foo or /Users/ola/bar – os.path.join(dir,sub) returns full path – Platform independent paths

• What's the difference between file and folder? – os.path.isdir() and os.path.getsize() compsci101 spring16 13 compsci101 spring16 14

Recursion in Pictures Mystery Recursion • http://xkcd.com/688/ and bit.ly/101sp16-0414-2 http://xkcd.com/543/

compsci101 spring16 15 compsci101 spring16 16 Something Recursion Hand back Exam 2 bitly/101sp16-0414-3

compsci101 spring16 17 compsci101 spring16 18