Lecture 9: Fun with Files
Craig Zilles (Computer Science) October 25, 2019 https://go.illinois.edu/cs105fa19
CS 105 Lecture 9: Fun with Files Craig Zilles (Computer Science) - - PowerPoint PPT Presentation
CS 105 Lecture 9: Fun with Files Craig Zilles (Computer Science) https://go.illinois.edu/cs105fa19 October 25, 2019 Are you sitti ting next t to someone to talk to for th the clicker questi tions? To Today I'm using: a text editor and
Craig Zilles (Computer Science) October 25, 2019 https://go.illinois.edu/cs105fa19
2
3
4
ls ls -l
dir
cat whole file at once
5
6
Example program 1: Diary that records user input into a file. with open('filename', 'w') as outf: closes file when code block ends
7
8
A) open('filename', 'r') B) open('filename', 'x') C) open('filename', 'i') D) open('filename', 'a') E) open('filename', 'e')
9
Processing a CSV manually:
Example program 2: Create a list of Illinois U.S. representatives
10
11
12
A) for line in lines[:1]: B) for line in lines[1:]: C) for line in lines[:2]: D) for line in lines[2]: E) for line in lines[2:]:
13
newlist = [] for thing in collection: if thing meets criteria: newlist.append(thing)
14
15
16
17
current_best = a value you know is worse than best for thing in collection: if thing is better than current_best: current_best = thing return / do something with current_best
18
current_best = a value you know is worse than best best_info = None for thing in collection: if thing is better than current_best: current_best = thing best_info = info about thing return / do something with current_best, best_info
19
import sys sys.argv is a list of arguments
20