1 / 17

Files

Learn how to efficiently read, write, and process files in Python using file objects. Explore different methods to work with files, skip lines, and write to files. Discover the basics of intro to classes and data structures in Python programming.

thsu
Download Presentation

Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files Victor Norman CS104

  2. Reading Quiz

  3. Using files • Before reading from or writing to a file, you have to open it. Returns a file object. • Reading: infile = open(“filename.txt”, “r”) • Creating file to write to: outfile = open(“filename.txt”, “w”) • Appending data to an existing file: outfile = open(“filename.txt”, “a”)

  4. File objects are iterable • A file object is iterable, so you can put it where <sequence> goes in a for statement: for line in inFile: print(line) • Note: line contains the ending newline each time. So, output shows a blank line between each line.

  5. Other ways to read a file • Read entire file into a single string:fileContents = dataFile.read() • Read file, line by line:line = dataFile.readline() • Note: if there are no more lines to read readline() returns empty string: “” • Read entire file into list of lineslines = dataFile.readlines()

  6. Typical use of files for data Book had this code in it: 1 infile = open("qbdata.txt", "r") 2 line = infile.readline() 3 while line != “”: 4 values = line.split() 5 print('QB', values[0], values[1], 'had a rating of', values[10]) 6 line = infile.readline() 7 8 infile.close() “priming read” Useful for processing data. values are strings. set up for next while test, but identical to previous line.

  7. Remove repeated readline() 1 infile = open("qbdata.txt", "r") 2 while True: • line = infile.readline() • if line == “”: break # done with loop: go to line 7 • values = line.split() • print('QB', values[0], values[1], 'had a rating of', values[10]) • infile.close()

  8. Skip lines in a file • What if there are blank lines you want to skip? infile = open("qbdata.txt", "r") while True: line = infile.readline() if line == “”: break # done with loop if line.strip() == “”: # had only whitespace continue # go to top of loop values = line.split() print('QB', values[0], values[1], 'had a rating of', values[10]) infile.close()

  9. Skip lines in a file • What if there are comment lines you want to skip? (Lines that start with #.) infile = open("qbdata.txt", "r") while True: line = infile.readline() if line == “”: break # done with loop if line.strip() == “”: # had only whitespace continue # go to top of loop if line.startsWith(“#”): # skip comments continue # go to top of loop values = line.split() print('QB', values[0], values[1], 'had a rating of', values[10]) infile.close()

  10. Writing to a file • To put data in a file: outfile.write(“The string to put there”) • Does not add a newline automatically, so you have to add \n. • E.g., to write last names, one per line: outfile = open(“lastnames.txt”, “w”) while … some code …: lastName = … some code … outfile.write(lastName + “\n”) outfile.close()

  11. Intro to Classes

  12. “Records” • In Excel, you can create rows that represent individual things, with each column representing some property of that thing. • E.g., each row could represent a student, with • column 1: student id • column 2: student last name • column 3: student first name • column 4: gpa • column 5: how much tuition is owed… • Each row *must* stay together: don’t want to move values from one row to another.

  13. How to do this in python? • How could we make a collection of items/values that belong together? • Have to use a composite data type. • i.e., lists or tuples. • Question: does order of items/values really matter?

  14. Coming attractions (lab this week) • A card is a tuple with 2 parts, a suit (one of “s”, “d”, “c”, “h”) and a number (2 – 14). • We create a card by making a tuple. • We access the suit via card[0] and number via card[1]. • What is good and what is bad about this implementation?

  15. What types of variables can we make? • Is this good enough? Wouldn’t it be nice if we could create our own types?

  16. Big Question What defines a type? • Data + operations • what you can store. • what you can do to or with it.

  17. Terminology • a class is like a recipe (or template). • you don't eat the recipe, right? • an object is an instantiation of that class • that's what you eat. • Or, a class is a new type. • Each class is defined by its • name • attributes (characteristics, properties, fields) • methods (functions) • We already know how to define functions, but we don’t know how to group them together, to say, “These belong together, and they operate on this data.”

More Related