CS 1111 Introduction to Programming Spring 2019

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
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.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
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 Caution about readlines vs. read and split.
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.
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.
Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
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.
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.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
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.
CHAPTER 3 File Output.
File Processing Upsorn Praphamontripong CS 1110
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Reading and writing files
Topic: File Input/Output (I/O)
File I/O File input/output Iterate through a file using for
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.
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
Python’s input and output
CSC 108H: Introduction to Computer Programming
Exceptions and files Taken from notes by Dr. Neil Moore
Class 9 Reading and writing to files chr, ord and Unicode
Chapter 5: Looping Starting Out with C++ Early Objects Eighth Edition
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
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
File I/O File input/output Iterate through a file using for
Exceptions and files Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Passing Parameters by value
File I/O File input/output Iterate through a file using for
CS 1111 Introduction to Programming Fall 2018
Introduction to Python: Day Three
Using Text Files in Python
CS190/295 Programming in Python for Life Sciences: Lecture 3
Topics Introduction to File Input and Output
CPS120: Introduction to Computer Science
Python I/O Peter Wad Sackett.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CSE 231 Lab 5.
Topics Introduction to File Input and Output
CSC 221: Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Spring 2019
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

CS 1111 Introduction to Programming Spring 2019 File Processing CS 1111 Introduction to Programming Spring 2019

Overview: File Processing Repeated process Open a file Read contents of a file Process the content Write/append content to another file Close a file Open before read or write Open before close Read or write before close Be extra careful when writing or appending the files !!

Opening Files (Local): open() file_variable = open(filename, mode) The location of a file – specify a path to a file If a data file and program are in different folders, use an absolute path If a data file and program are in the same folder, use a relative path For CS 1110/1111 homework submission/testing purpose and to avoid a “file not found” problem, put all data files in the same folder as your python files and specify a relative path. Example test_file1 = open('myfile.txt', 'r') # relative path test_file2 = open('c:/cs1111/myfile.txt', 'r') # absolute path ‘r’ = read only (default mode) ‘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)

Opening Files Open with a “read” mode Open with a “append” mode a “write” mode File content Here is some text that we can combine with another file. File content Here is some text that we can combine with another file.

With open with open (filename) as handle_name: statements # check if a file is csv and exists with open (filename) as handle_name: statements The file will be closed automatically after the statements block def read_file_with_open(filename): with open (filename) as infile: file_contents = infile.read() print(file_contents)

Reading from Files: readline() 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() Read one line

Reading from Files: readline() 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()

Reading from Files: read() 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() Read the entire file Read from the beginning of the file until the end of file, EOF (i.e., empty string)

Reading from Files: readlines() and read() Read all the lines in the file and return a list of strings file_variable = open(filename, 'r') buffer = file_variable.readlines() buffer = file_variable.read() def read_file(): # open students.txt to read infile = open('students.txt', 'r') # read the file's contents file_contents = infile.readlines() for line in file_contents: print(line) # close the file infile.close() def read_file(): # open students.txt to read infile = open('students.txt', 'r') # read the file's contents file_contents = infile.read() for line in file_contents: print(line) # close the file infile.close()

Writing to Files: write() 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

Appending to Files: write() file_variable = open(filename, 'a') file_variable = write(string_to_be_written) Where is this file? What does it look like? 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() 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

Closing Files: close() 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()

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

Validating a File from os.path import * # get file name file_name = input("Enter csv file name: ") # check if a file is csv 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: ")

Summary Must know (based on exam2 topic list, as of 03/25/2019) Read from files open(filename) connection.read() connection.readline() ways of iterating lines connection.readlines() connection.read().split('\n') for line in connection