Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.

Slides:



Advertisements
Similar presentations
Operating System.
Advertisements

Computer Science 111 Fundamentals of Programming I Files.
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Systems Software Operating Systems.
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Basic Instructions on how to use One Drive and share files. ONE Drive Your LogoYour own footer.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Systems Software Operating Systems. What is software? Software is the term that we use for all the programs and data that we use with a computer system.
PowerPoint Extras. Eyes to the front please! Action Buttons.
Introduction to Python Lesson 1 First Program. Learning Outcomes In this lesson the student will: 1.Learn some important facts about PC’s 2.Learn how.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Multimedia Summer Camp
Lesson 06: Functions Class Participation: Class Chat:
Lesson 03: Variables and Types
Topic: File Input/Output (I/O)
COMP Streams and File I/O
Fundamentals of Python: First Programs
Exam #1 You will have exactly 30 Mins to complete the exam.
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Writing & reading txt files with python 3
Lesson 07: Strings Class Chat: Attendance: Participation
Lesson 10: Dictionaries Topic: Introduction to Programming, Zybook Ch 9, P4E Ch 9. Slides on website.
Python Let’s get started!.
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Lesson 02: Introduction to Python
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Lesson 1 An Introduction
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lesson 05: Iterations Class Chat: Attendance: Participation
IST256 : Applications Programming for Information Systems
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 13: File Input and Output
And now for something completely different . . .
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Reading and Writing Files
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Files
Adapted from slides by Marty Stepp and Stuart Reges
Exceptions and files Taken from notes by Dr. Neil Moore
Lesson 09: Lists Topic: Introduction to Programming, Zybook Ch 8, P4E Ch 8. Slides on website.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
IST256 : Applications Programming for Information Systems
Lesson 06: Functions Class Chat: Attendance: Participation
All examples are based upon Visual C++, version 5
Using Text Files in Python
Lesson 09: Lists Class Chat: Attendance: Participation
Lesson 08: Files Class Chat: Attendance: Participation
Random Access Files / Direct Access Files
Lesson 10: Dictionaries Class Chat: Attendance: Participation
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Bryan Burlingame 17 April 2019
Lesson 02: Introduction to Python
CSE 231 Lab 5.
Topics Introduction to File Input and Output
CYB 130 RANK Dreams Come True / cyb130rank.com.
Lesson 07: Strings Class Chat: Attendance: Participation
IST256 : Applications Programming for Information Systems
Presentation transcript:

Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.

Agenda The importance of a persistence layer in programming. How to read and write from files. Techniques for reading a file a line at a time. Using exception handling with files. You’ve Read: Zybook Ch7 P4E Ch7

Connect Activity Which of the following is not an example of secondary (persistent) memory? Flash Memory Hard Disk Random-Access Memory DVD-ROM

Files == Persistence Files add a Persistence Layer to our computing environment where we can store our data after the program completes. Think: Saving a game! When our program Stores data, we open the file for writing. When our program Reads data, we open the file for reading. To read or write a file we must first open it, which gives us a special variable called a file handle. We then use the file handle to read or write from the file. The read() function reads from the write() function writes to the file through the file handle.

Reading From a File Writing a To File filename – String name of file as found by the operating system handle – A Python variable representing the filename. Only available under with open. When you write to a file, whatever contents were there get overwritten. with open(filename,'r') as handle: string-text = handle.read() Writing a To File with open(filename,'w') as handle: handle.write(string-text)

Watch Me Code 1 Let’s Write two programs. Save a text message to a file. Retrieve the text message from the file. # Input Password from user and write it to a file. password = input("Enter your new password:") with open ("password.txt","w") as file: file.write(password) print("Password Saved!") def readpassword(): with open("password.txt","r") as file: password = file.read() return password password = readpassword() print (password)

Check Yourself: Which line 1 Choose the line number that creates the file handle?

Watch Me Code 2 Common patterns for reading and writing more than one item to a file. Input a series of grades, write them to a file one line at a time. Read in that file one line at a time, print average. # Input Password from user and write it to a file. password = input("Enter your new password:") with open ("password.txt","w") as file: file.write(password) print("Password Saved!") def readpassword(): with open("password.txt","r") as file: password = file.read() return password password = readpassword() print (password)

Check Yourself: Which line 2 Choose first line number in which the file handle no longer exists:

Your Operating System and You Files are stored in your secondary memory in folders. When the python program is in the same folder as the file, no path is required. When the file is in a different folder not, a path is required. Absolute paths point to a file starting at the root of the hard disk. Relative paths point to a file starting at the current place on the hard disk.

Python Path Examples What Windows Mac / Linux File in current folder "file.txt" File up one folder from the current folder "../file.txt" File in a folder from the current folder "folder1/file.txt" Absolute path to file in a folder "C:/folder1/file.txt" "/folder1/file.txt"

Check Yourself: Path Is this path relative or absolute? "/path/to/folder/file.txt"

Handling Errors with Try…Except Don't assume you can read a file! Use try… except!

End-To-End Example How Many Calories in that Beer? Let's write a program to search a data file of 254 popular beers. Given the name of the beer the program will return the number of calories.

In Class Coding Lab: The goals of this lab are to help you to understand: Reading data from a file all at once or one line at a time. Searching for data in files Parsing text data to numerical data. How to build complex programs incrementally.