CS 105 Lecture 9: Fun with Files Craig Zilles (Computer Science) - - PowerPoint PPT Presentation

cs 105
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Lecture 9: Fun with Files

Craig Zilles (Computer Science) October 25, 2019 https://go.illinois.edu/cs105fa19

CS 105

slide-2
SLIDE 2

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 the command line Also, I'll post today's code

2

slide-3
SLIDE 3

To Today

  • 1. Files
  • Extensions,
  • Writing, flushing, closing
  • 2. Comma-separated values (CSV) files
  • Reading, splitting, filter & find best patterns
  • csv library
  • 3. More on Lists
  • 4. List Comprehesions

3

slide-4
SLIDE 4

Fi Files es

  • Files are how data is stored on external storage (disk)
  • Managed by O/S
  • Disk is slow
  • opening files moves them to memory for the program
  • Data is buffered (temporarily stored) in memory

4

slide-5
SLIDE 5

Vi Viewing ng the he filesystem

  • Mac Finder / Windows explorer
  • Command line:
  • Mac:

ls ls -l

  • Windows:

dir

  • Shows all of the files in the current directory
  • Can show file size
  • Looking at files (Mac): more
  • ne page at a time

cat whole file at once

5

slide-6
SLIDE 6

Write programs a few lines at t a ti time! Test t every couple lines to make sure th they do what t you want! t!

6

slide-7
SLIDE 7

Wr Writing to files

  • file_object = open('filename', 'w')
  • file_object.write('thing to write')
  • file_object.close() automatic at program end
  • file_object.flush()
  • ptional

Example program 1: Diary that records user input into a file. with open('filename', 'w') as outf: closes file when code block ends

7

slide-8
SLIDE 8

Di Diar ary that records user input into a a file.

8

slide-9
SLIDE 9

Co Continue e writing g to exi xisting g file? e? (i.e., new ew writes go to end of file)

A) open('filename', 'r') B) open('filename', 'x') C) open('filename', 'i') D) open('filename', 'a') E) open('filename', 'e')

9

slide-10
SLIDE 10

Com Comma-sepa separated ed value ue (CSV) files es

  • Commonly-used data file format
  • Can be generated from Excel and other spreadsheets

Processing a CSV manually:

  • Each row is its own line of the file
  • Can use split(',') to separate the columns
  • Use indexing to read columns of interest

Example program 2: Create a list of Illinois U.S. representatives

10

slide-11
SLIDE 11

Cr Create a lis list t of

  • f Illin

llinois

  • is U.S

.S. . representativ tives

11

slide-12
SLIDE 12

Re Reading from files

  • file_object = open('filename')
  • lines = file_object.readlines()
  • for line in lines:

12

slide-13
SLIDE 13

Sk Skipping first t line

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

slide-14
SLIDE 14

Fi Filter ering a a collec ection (p (patter ern)

newlist = [] for thing in collection: if thing meets criteria: newlist.append(thing)

14

slide-15
SLIDE 15

Sor Sorti ting data

  • a_list.sort()
  • a_list.sort(key = a_function)
  • a_list.sort(reverse = True)

15

slide-16
SLIDE 16

An Annou

  • unce

cements ts

  • Exam 2 is ongoing
  • Please don't start early; listen to the proctors
  • Informal Early Feedback (IEF)
  • Have results of scantron
  • Still processing written responses
  • Will discuss next week

16

slide-17
SLIDE 17

Fi Find da nd date o

  • f hi

f highe hest une unempl ploym yment r rate

  • Example 3

17

slide-18
SLIDE 18

Pa Pattern: Finding best in a collection

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

slide-19
SLIDE 19

Fi Finding info asso associated ed with bes est

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

slide-20
SLIDE 20

Com Command line arguments ts

  • Avoid hard coding things like file names

import sys sys.argv is a list of arguments

  • First argument is the name of the Python script
  • Give 'Usage' message to help user use your program

20