File Processing Upsorn Praphamontripong CS 1110

Slides:



Advertisements
Similar presentations
Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
Advertisements

File I/O Dr. Nancy Warter-Perez. Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via
Computer Science 111 Fundamentals of Programming I Files.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Topics This week: File input and output Python Programming, 2/e 1.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Python programs How can I run a program? Input and output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Chapter 8 Data File Basics.
Course A201: Introduction to Programming 12/9/2010.
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.
Chapter 9 I/O Streams and Data Files
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
Introduction to Java Files. Text Files A sequential collection of data stored on a permanent storage device Hard drive USB memory CD/DVD Has a name and.
Files Tutor: You will need ….
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
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])
Learners Support Publications Working with Files.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
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.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Reading and writing files
Topic: File Input/Output (I/O)
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
Introduction to Programming
Input and Output Upsorn Praphamontripong CS 1110
File I/O File input/output Iterate through a file using for
File Writing Upsorn Praphamontripong CS 1110
INPUT AND OUTPUT.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CSc 120 Introduction to Computer Programing II
IST256 : Applications Programming for Information Systems
Exceptions and files Taken from notes by Dr. Neil Moore
Class 9 Reading and writing to files chr, ord and Unicode
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Reading and Writing Files
Using files Taken from notes by Dr. Neil Moore
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
Adapted from slides by Marty Stepp and Stuart Reges
Exceptions and files Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
File I/O File input/output Iterate through a file using for
Video list editor BIS1523 – Lecture 24.
CS 1111 Introduction to Programming Fall 2018
Introduction to Python: Day Three
Introduction to Programming
Lesson 08: Files Class Chat: Attendance: Participation
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
Formatted Input, Output & File Input, Output
Topics Introduction to File Input and Output
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
CSE 231 Lab 5.
Introduction to Programming
Topics Introduction to File Input and Output
CSC 221: Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Spring 2019
CS 1111 Introduction to Programming Spring 2019
CS 1111 Introduction to Programming Spring 2019
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

File Processing Upsorn Praphamontripong CS 1110 Introduction to Programming Spring 2017

Overview: File Input and Output Opening files Reading from files Writing to files Appending to files Closing files CS 1110

Opening Files (Local) Opening a file The location of a file Example file_variable = open(filename, mode) The location of a file In general, we need to specify a path to a file. If a data file and your program are in different folders, an absolute path must be used. If a data file and your program is in the same folder, a relative path can be used. In this course, for homework submission/testing purpose and to avoid a “file not found” problem, you *need* to put all data files in the same folder as your python files and specify a relative path. Example test_file = open('test.txt', 'r') # relative path ‘r’ = read only ‘w’ = write (If the file exists, erase its contents. If it does not exist, create a new file to be written to) ‘a’ = append a file (If the file exists, append data to the file. If the file does not exist, create a new file) CS 1110

Reading from Files: readline( ) Reading from a file file_variable = open(filename, 'r') file_variable.readline() def readline_file(): # open a file named students.txt to be read infile = open('students.txt', 'r') # read 2 lines from the file line1 = infile.readline() line2 = infile.readline() print(line1, line2) print(line1, line2) # close the file infile.close() CS 1110

Reading from Files: readline( ) with loops Reading from a file with loops file_variable = open(filename, 'r') file_variable.readline() def readline_file_with_loop(): # open a file named students.txt to be read infile = open('students.txt', 'r') line = infile.readline() while line != '': print(line.rstrip('\n')) line = infile.readline() # what happen if this line is commented out # close the file infile.close() CS 1110

Reading from Files: read( ) Reading from a file file_variable = open(filename, 'r') file_variable.read() def read_file(): # open a file named students.txt to be read infile = open('students.txt', 'r') # read the file's contents file_contents = infile.read() print(file_contents) # close the file infile.close() CS 1110

Writing to Files Writing to a file file_variable = open(filename, 'w') file_variable = write(string_to_be_written) def main(): # open a file named students.txt outfile = open('students.txt', 'w') # write the names of three students to the file outfile.write('John\n') outfile.write('Jack\n') outfile.write('Jane\n') # close the file outfile.close() Where is this file? What does it look like? John\nJack\nJane\n Beginning of the file End of the file CS 1110

Appending to Files Appending to a file file_variable = open(filename, 'a') file_variable = write(string_to_be_written) def main(): # open a file named students.txt outfile = open('students.txt', 'a') # write the names of three students to the file outfile.write('Mary\n') # close the file outfile.close() Where is this file? What does it look like? John\nJack\nJane\n Beginning of the file End of the file John\nJack\nJane\nMary\n Beginning of the file End of the file CS 1110

Closing Files Closing a file file_variable.close() def read_file(): # open a file named students.txt to be read infile = open('students.txt', 'r') # read the file's contents file_contents = infile.read() print(file_contents) # close the file infile.close() CS 1110

Wrap Up File Operations argument Function header (or signature) Why isn’t a file opened with a “write” mode ? If we want to open a file with a “write” mode, how should we modify the code ? def read_list_of_names(filename): names = [] datafile = open(filename, "r") outfile = open(filename, "a") for line in datafile: line = line.strip() names.append(line) outfile.write(line) datafile.close() outfile.close() return names print(read_list_of_names("names.txt")) Open a file with a “read” mode Open a file with an “append” mode For each line in datafile Strip leading and tailing spaces Append string to a list Write string to outfile (must be string) Close files Value-return function Function call parameter names CS 1111

Validating a File from os.path import * # get file name file_name = input("Enter csv file name: ") # check if a file is csv, is valid, and exists while (not file_name.endwith("csv") or not isfile(file_name) ): file_name = input(file_name + " is not csv or does not exist. Enter csv file name: ") CS 1111

Exercise (Local File) Down the following file and save it as “weather.csv” in the same folder where you have your .py file (Do not double click!! Do not open it with Excel!!) Charlottesville Weather for September 2015 - http://cs1110.cs.virginia.edu/code/cville_weather_sept15.csv Write a function to read the weather data and compute the average “Max TemperatureF” of Sep 2015. Hint: burn header, use split(“,”), and cast data CS 1111

Opening Files (Internet, via URLs) import urllib.request link = input ( 'Web page: ' ) stream = urllib.request.urlopen( link ) for line in stream: decoded = line.decode("UTF-8") print(decoded.strip()) CS 1111

Exercise (URL) Write a function to read a dataset from the Internet and display the data on the screen. This dataset contains history temperature. (Do not save the dataset to your computer) http://www.wunderground.com/history/airport/KCHO/2012/03/17/DailyHistory.html?format=1 Hint: use urllib, decode("UTF-8"), strip(), then split() CS 1111