cs 105
play

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


  1. CS 105 Lecture 9: Fun with Files Craig Zilles (Computer Science) https://go.illinois.edu/cs105fa19 October 25, 2019

  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

  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

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

  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 one page at a time whole file at once cat 5

  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

  7. Wr Writing to files • file_object = open('filename', 'w') • file_object.write('thing to write') • file_object.close() automatic at program end optional • file_object.flush() 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. Di Diar ary that records user input into a a file. 8

  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

  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

  11. Cr Create a lis list t of of Illin llinois ois U.S .S. . representativ tives 11

  12. Re Reading from files • file_object = open('filename') • lines = file_object.readlines() • for line in lines: 12

  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

  14. Fi Filter ering a a collec ection (p (patter ern) newlist = [] for thing in collection : if thing meets criteria : newlist .append( thing ) 14

  15. Sor Sorti ting data • a_list.sort() • a_list.sort(key = a_function) • a_list.sort(reverse = True) 15

  16. An Annou ounce 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

  17. Fi Find da nd date o of hi f highe hest une unempl ploym yment r rate • Example 3 17

  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

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend