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.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Section 6.1 CS 106, Fall The Big Q What do we get by being able to define a class?! Or Do we really need this?!
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
While loops.
CS104: Chapter 15 CS 104 Students and me. Big Question Q: You say creating a class is how to create a new type. Why would we even want to do this?! A:
Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Designing a Database Unleashing the Power of Relational Database Design.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Lists in Python.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
1 CS161 Introduction to Computer Science Topic #13.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Extra Stuff for CS106 Victor Norman CS106. break and continue break and continue are both ways to alter the flow of a loop Can be used only inside a loop.
Planning & Creating a Database By Ms. Naira Microsoft Access.
Files Tutor: You will need ….
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures.
Python I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Files in Python Opening and Closing. Big Picture To use a file in a programming language – You have to open the file – Then you process the data in the.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
File Processing CMSC 120: Visualizing Information Lecture 4/15/08.
More about Iteration Victor Norman CS104. Reading Quiz.
File Processing Upsorn Praphamontripong CS 1110
Module 5 Working with Data
BIT116: Scripting Loops.
File I/O File input/output Iterate through a file using for
File Writing Upsorn Praphamontripong CS 1110
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CSc 120 Introduction to Computer Programing II
Department of Computer Science,
Class 9 Reading and writing to files chr, ord and Unicode
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Reading and Writing Files
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
File I/O File input/output Iterate through a file using for
CS190/295 Programming in Python for Life Sciences: Lecture 6
File I/O File input/output Iterate through a file using for
CS 1111 Introduction to Programming Fall 2018
Python Lists and Sequences
Topics Introduction to File Input and Output
Python I/O Peter Wad Sackett.
“Everything Else”.
Topics Introduction to File Input and Output
CS 1111 Introduction to Programming Spring 2019
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

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

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

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.

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 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 lines lines = 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 7 5 values = line.split() 6 print('QB', values[0], values[1], 'had a rating of', values[10]) 7infile.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.”