Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files Victor Norman CS104. Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled.

Similar presentations


Presentation on theme: "Files Victor Norman CS104. Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled."— Presentation transcript:

1 Files Victor Norman CS104

2 Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled

3 Reading Quiz, Q2 What is this? \n A.A continuation character B.An end-of-file character C.A newline character D.An end-of-line character

4 Reading Quiz, Q3 When you open a file for reading or writing, python gives you back a … A.file object B.file descriptor C.file name D.None of the above.

5 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”)

6 File objects are iterable A file object is iterable, so you can put it where 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.

7 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 lines lines = dataFile.readlines()

8 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” set up for next while test, but identical to previous line. Useful for processing data. values are strings.

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

10 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()

11 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()

12 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()

13 Intro to Classes

14 “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.

15 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?

16 Ancient History (last Thursday) 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?

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

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

19 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.”


Download ppt "Files Victor Norman CS104. Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled."

Similar presentations


Ads by Google