files victor norman cs104. reading quiz, q1 a file must be ___________ before a program can read...
TRANSCRIPT
Files
Victor NormanCS104
Reading Quiz, Q1
A file must be ___________ before a program can read data from it.
A. accessedB. unlockedC. openedD. controlled
Reading Quiz, Q2
What is this? \n
A. A continuation characterB. An end-of-file characterC. A newline characterD. An end-of-line character
Reading Quiz, Q3
When you open a file for reading or writing, python gives you back a …
A. file objectB. file descriptorC. file nameD. None of the above.
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”)
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.
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()
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.
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 75 values = line.split()6 print('QB', values[0], values[1],
'had a rating of', values[10])7 infile.close()
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()
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()
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()
Intro to Classes
“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.
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?
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?
What types of variables can we make?
• Is this good enough?
Wouldn’t it be nice if we could create our own types?
Big Question
What defines a type?
• Data + operations– what you can store.– what you can do to or with it.
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.”