Reading and Writing Files

Slides:



Advertisements
Similar presentations
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007.
Chapter 5: Loops and Files.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
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.
Topics This week: File input and output Python Programming, 2/e 1.
Bits and Data Storage. Basic Hardware Units of a Computer.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Today’s Agenda: Computer Basics Review Hardware: The physical components of a computer, any internal or external computer part that you can touch. Software:
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Topics Introduction Hardware and Software How Computers Store Data
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
CIS 270—Application Development II Chapter 14—Files and Streams.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
Chapter 8 Data File Basics.
Chapter 9 I/O Streams and Data Files
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Computer Architecture
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
 Pearson Education, Inc. All rights reserved Files and Streams.
The Computer System CS 103: Computers and Application Software.
CIS 234: File Input & Output introduction. Data Storage Is Fundamental computers need to store data to work with it memory (RAM) is fast but transient.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
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])
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
CHAPTER 1 COMPUTER SCIENCE II. HISTORY OF COMPUTERS (1.1) Eniac- one of the worlds first computers Used more electricity than an entire city block of.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 1: Introduction to Computers and Programming
File Processing Upsorn Praphamontripong CS 1110
BASIC PROGRAMMING C SCP1103 (02)
Agenda Introduction Computer Programs Python Variables Assignment
Computer Science II Chapter 1.
Basic Computer Vocabulary
Topic: File Input/Output (I/O)
Topics Introduction Hardware and Software How Computers Store Data
File I/O File input/output Iterate through a file using for
BASIC PROGRAMMING C SCP1103 (02)
File Writing Upsorn Praphamontripong CS 1110
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Files.
Overview of Computers & Programming Languages
Chapter 1: Introduction to Computers and Programming
JAVA IO.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Topics Introduction Hardware and Software How Computers Store Data
MSIS 670: Object-Oriented Software Engineering
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Topics Introduction Hardware and Software How Computers Store Data
CS 1111 Introduction to Programming Fall 2018
Topics Introduction to File Input and Output
File Input and Output.
Chapter 15 Files, Streams and Object Serialization
Topics Introduction to File Input and Output
CS 1111 Introduction to Programming Spring 2019
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

Reading and Writing Files

Using Files for Data Storage Allows data to be retained between program runs It’s lost when a local variable goes out of scope or when the program terminates Computers store files on secondary storage devices hard disks, optical disks, flash drives and magnetic tapes. Data maintained in files is persistent data because it exists beyond the duration of program execution. Used in many applications: Word processing, Image Editors, Databases, Spreadsheets, Compilers, Games and Web Browsers File: a set of data stored on a computer, often on a disk drive Programs can read from and/or write to files Steps: Open the file Use the file (read from, write to, or both) Close the file

Data Hierarchy The smallest data item in a computer can assume the value 0 or 1. Such data items are called bits Python uses Unicode characters that are composed of two bytes, each composed of eight bits(lang) Fields are composed of characters or bytes. Data items processed by computers form a data hierarchy that becomes larger and more complex in structure as we progress from bits to characters to fields, and so on. Typically, several fields compose a record (implemented as a class in Java). A file is a group of related records.  Database

Opening a File Stream The first part of read/write a file is to create a stream to the file. A stream is a one way path for information to travel Code: varName = open(fileName, mode) Ex. fileWrite = open(“test.txt”,”w”) input=open(“c:\\py\\tst.txt”,”r”)

Writing Files To write to a file use the file stream name like a variable with the write function. Code: write(str) Ex. output = open(“city.txt”) output.write(“blah blah blah\n”) Note: Loops can be used to write numerous values to a file

Reading Files Reading a file is similar to writing a file in that you use the file stream name followed by one of the read functions. Code: read() – reads all characters from a file read(#) – read a number of characters readline() – reads all characters up to and including \n readlines() – reads all lines

Reading Data From a File Two main ways to read data from a file: 1. While loop that iterates to the end of the file Code: line = infile.readline() while line != ‘’: #process line here line = infile.readline() 2. A enhanced for loop Code: for varname in fileName #process the line here

Closing a File Stream Clean up is a necessary and essential step. Just like when you make a mess, life will go on but there are implications of not cleaning up. Ensure you close a stream after you have processed all of your data. Code: fileStream.close()

Reading and Writing Files Example def main(): # Prompt the user to enter filenames f1 = input("Enter a source file: ").strip() f2 = input("Enter a target file: ").strip() # Open files for input and output infile = open(f1, "r") outfile = open(f2, "w") # Copy from input file to output file countLines = countChars = 0 for line in infile: countLines += 1 countChars += len(line) outfile.write(line) print(countLines, "lines and", countChars, "chars copied") infile.close() # Close the input file outfile.close() # Close the output file main() # Call the main function

Checkpoint 1. What are the 3 phases to a file stream? 2. What are some different ways to setup your file stream? 3. What are the key differences between a read and write file stream?