File I/O Ruth Anderson UW CSE 140 Winter 2014 1. File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Computer Science 111 Fundamentals of Programming I Files.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
Last Week Looping though lists Looping using range While loops.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lists Introduction to Computing Science and Programming I.
Lecture 9: SHELL PROGRAMMING (continued) Creating shell scripts!
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.
An Introduction to Operating Systems. Definition  An Operating System, or OS, is low-level software that enables a user and higher-level application.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Python programs How can I run a program? Input and output.
CS0007: Introduction to Computer Programming File IO and Recursion.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
More on Functions; Some File I/O UW CSE 190p Summer 2012.
File I/O Michael Ernst UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
5 1 Data Files CGI/Perl Programming By Diane Zak.
CS2021 Week 2 Off and Running with Python. Two ways to run Python The Python interpreter – You type one expression at a time – The interpreter evaluates.
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 – reading and writing files. ??? ???two ways to open a file open and file ??? How to write to relative and absolute paths?
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
The Python interpreter CSE 160 University of Washington Ruth Anderson 1.
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])
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
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.
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.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Programming with ANSI C ++
Topic: File Input/Output (I/O)
Introduction to Programming
CSC 131: Introduction to Computer Science
The Python interpreter
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
(optional - but then again, all of these are optional)‏
Computer Organization & Assembly Language Chapter 3
Ruth Anderson UW CSE 160 Winter 2016
Exceptions and files Taken from notes by Dr. Neil Moore
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Spring 2018
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Topics Introduction to File Input and Output
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
The Python interpreter
Fundamentals of Programming I Files
File Handling.
Exceptions and files Taken from notes by Dr. Neil Moore
CS703 - Advanced Operating Systems
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Introduction to Computer Science
Topics Introduction to File Input and Output
The Python interpreter
The Python interpreter
File Handling.
Introduction to Computer Science
Presentation transcript:

File I/O Ruth Anderson UW CSE 140 Winter

File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with a file? 2

Files store information when a program is not running Important operations: open a file close a file read data write data 3

Files and filenames A file object represents data on your disk drive – Can read from it and write to it A filename (usually a string) states where to find the data on your disk drive – Can be used to find/create a file – Examples: Linux/Mac: "/home/rea/class/140/lectures/file_io.pptx" Windows: "C:\Users\rea\My Documents\cute_dog.jpg" Linux/Mac: "homework3/images/Husky.png" "Husky.png" 4

Two types of filenames An Absolute filename gives a specific location on disk: "/home/rea/class/140/14wi/lectures/file_io.pptx" or "C:\Users\rea\My Documents\homework3\images\Husky.png" – Starts with “/” (Unix) or “C:\” (Windows) – Warning: code will fail to find the file if you move/rename files or run your program on a different computer A Relative filename gives a location relative to the current working directory: "lectures/file_io.pptx" or " images\Husky.png" – Warning: code will fail to find the file unless you run your program from a directory that contains the given contents A relative filename is usually a better choice 5

Examples Linux/Mac: These could all refer to the same file: "/home/rea/class/140/homework3/images/Husky.png" "homework3/images/Husky.png" "images/Husky.png" "Husky.png“ Windows: These could all refer to the same file: "C:\Users\rea\My Documents\class\140\homework3\images\Husky.png" "homework3\images\Husky.png" "images\Husky.png" "Husky.png" 6

“Current Working Directory” in Python The directory from which you ran Python To determine it from a Python program: >>> import os # "os" stands for "operating system" >>> os.getcwd() '/Users/johndoe/Documents' Can be the source of confusion: where are my files? 7

Reading a file in python # Open takes a filename and returns a file. # This fails if the file cannot be found & opened. myfile = open("datafile.dat") # Approach 1: for line_of_text in myfile: … process line_of_text # Approach 2: all_data_as_a_big_string = myfile.read() myfile.close() # close the file when done reading Assumption: file is a sequence of lines Where does Python expect to find this file (note the relative pathname)? 8

Reading a file Example # Count the number of words in a text file in_file = "thesis.txt" myfile = open(in_file) num_words = 0 for line_of_text in myfile: word_list = line_of_text.split() num_words += len(word_list) myfile.close() print "Total words in file: ", num_words 9

Reading a file multiple times You can iterate over a list as many times as you like: mylist = [ 3, 1, 4, 1, 5, 9 ] for elt in mylist: … process elt for elt in mylist: … process elt Iterating over a file uses it up: myfile = open("datafile.dat") for line_of_text in myfile: … process line_of_text for line_of_text in myfile: … process line_of_text How to read a file multiple times? Solution 1: Read into a list, then iterate over it myfile = open("datafile.dat") mylines = [] for line_of_text in myfile: mylines.append(line_of_text) … use mylines Solution 2: Re-create the file object (slower, but a better choice if the file does not fit in memory) myfile = open("datafile.dat") for line_of_text in myfile: … process line_of_text myfile = open("datafile.dat") for line_of_text in myfile: … process line_of_text 10 This loop body will never be executed!

Writing to a file in python # Replaces any existing file of this name myfile = open("output.dat", "w") # Just like print ing output myfile.write("a bunch of data") myfile.write("a line of text\n") myfile.write(4) myfile.write(str(4)) myfile.close() open for Writing (no argument, or "r", for Reading) “\n” means end of line (Newline) Wrong; results in: TypeError: expected a character buffer object Right. Argument must be a string 11 close when done with all writing