Lesson 08: Files Class Chat: Attendance: Participation

Slides:



Advertisements
Similar presentations
Computer Science 111 Fundamentals of Programming I Files.
Advertisements

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.
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.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
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.
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.
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:
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
Lesson 11: Web Services and API's
Multimedia Summer Camp
Lesson 06: Functions Class Participation: Class Chat:
Lesson 12: Data Analysis Class Participation: Class Chat:
Lesson 03: Variables and Types
Topic: File Input/Output (I/O)
COMP Streams and File I/O
Whatcha doin'? Aims: To start using Python. To understand loops.
Exam #1 You will have exactly 30 Mins to complete the exam.
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
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.
Lesson 13: Visualizations
Operating System.
Week of 12/12/16 Test Review.
Python Let’s get started!.
IST256 : Applications Programming for Information Systems
Lesson 02: Introduction to Python
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lesson 05: Iterations Class Chat: Attendance: Participation
IST256 : Applications Programming for Information Systems
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 . . .
Lesson 13: Visualizations
Fill the screen challenge!
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lesson 04: Conditionals Class Chat: Attendance: Participation
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
File IO and Strings CIS 40 – Introduction to Programming in Python
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Fundamentals of Programming I Files
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.
Python Lessons 13 & 14 Mr. Kalmes.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
IST256 : Applications Programming for Information Systems
Lesson 06: Functions Class Chat: Attendance: Participation
Lesson 09: Lists Class Chat: Attendance: Participation
Lesson 03: Variables and Types
Lesson 11: Web Services and API's
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 12: Data Analysis Class Chat: Attendance: Participation
Lesson 02: Introduction to Python
Topics Introduction to File Input and Output
CYB 130 RANK Dreams Come True / cyb130rank.com.
Introduction Time is something we waist as a society
Lesson 07: Strings Class Chat: Attendance: Participation
IST256 : Applications Programming for Information Systems
IST256 : Applications Programming for Information Systems
Presentation transcript:

Lesson 08: Files Class Chat: Attendance: Participation Link: Gitter.im | Code: ???? Class Chat: https://gitter.im/IST256/Fudge Participation http://ist256.participoll.com/

Questions? Ask in Our Course Chat! 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 https://gitter.im/IST256/Fudge Questions? Ask in Our Course Chat!

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

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 or saving your work! 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 object 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 Which line number creates the file handle? A. 1 B. 2 C. 3 D. 4 A B C D

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 On which line number does the file handle no longer exist? A. 1 B. 2 C. 3 D. 4 A B C D

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.

Conclusion Activity "Confusion Says…" Share something you're still confused about from class today!