Ruth Anderson UW CSE 160 Spring 2018

Slides:



Advertisements
Similar presentations
compilers and interpreters
Advertisements

Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
Computer Science 111 Fundamentals of Programming I Files.
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.
CS0007: Introduction to Computer Programming File IO and Recursion.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
More on Functions; Some File I/O UW CSE 190p Summer 2012.
Chapter 9 I/O Streams and Data Files
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.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Files Tutor: You will need ….
Last Week Lists List methods Nested Lists Looping through lists using for loops.
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])
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
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.
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.
IIITD File Input / Output In Python. File and operations  File is a named location on disk to store related information  When we want to read from or.
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.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Programming with ANSI C ++
Topic: File Input/Output (I/O)
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
CSC201: Computer Programming
Module 5 Working with Data
Algorithmic complexity: Speed of algorithms
File I/O File input/output Iterate through a file using for
CSC 131: Introduction to Computer Science
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Computer Organization & Assembly Language Chapter 3
Managing results files
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 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Winter 2017
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
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
Intro to Computer Science CS1510 Dr. Sarah Diesburg
File Handling.
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
Introduction to Python: Day Three
Algorithmic complexity: Speed of algorithms
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
Stata Basic Course Lab 2.
15-110: Principles of Computing
Topics Introduction to File Input and Output
Algorithmic complexity: Speed of algorithms
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Bryan Burlingame 17 April 2019
Topics Introduction to File Input and Output
File Handling.
Introduction to Computer Science
Presentation transcript:

Ruth Anderson UW CSE 160 Spring 2018 File I/O Ruth Anderson UW CSE 160 Spring 2018

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

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

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

Two types of filenames An Absolute filename gives a specific location on disk: "/home/rea/class/160/18sp/lectures/file_io.pptx" "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 or 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" "images\Husky.png" "data\test-small.fastq" 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

Examples Linux/Mac: These could all refer to the same file: "/home/rea/class/160/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\160\homework3\images\Husky.png" "homework3\images\Husky.png" "images\Husky.png" "Husky.png"

Aside: “Current Working Directory” in Python Current Working Directory - the directory from which you ran Python To determine it from a Python program: import os print "The current working directory is", os.getcwd() Might print: '/Users/johndoe/Documents' os stands for “operating system”

Opening a file in python To open a file for reading: # Open takes a filename and returns a file object. # This fails if the file cannot be found & opened. myfile = open("datafile.dat") Or equivalently: myfile = open("datafile.dat", "r") To open a file for writing: # Will create datafile.dat if it does not already # exist, if datafile.dat already exists, then it # will be OVERWRITTEN myfile = open("datafile.dat", "w") # If datafile.dat already exists, then we will # append what we write to the end of that file myfile = open("datafile.dat", "a") By default, file is opened for reading

Reading a file in python # Open takes a filename and returns a file object. # This fails if the file cannot be found & opened. myfile = open("datafile.dat") # Approach 1: Process one line at a time for line_of_text in myfile: … process line_of_text # Approach 2: Process entire file at once 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)?

Simple Reading a file Example # Reads in file one line at a time and # prints the contents of the file. in_file = "student_info.txt" myfile = open(in_file) for line_of_text in myfile: print line_of_text myfile.close()

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

Reading a file multiple times In general, try to avoid reading a file more than on time. Reading files is slow. Reading a file multiple times 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) for line_of_text in mylines: … process line_of_text Solution 2: Re-create the file object (slower, but a better choice if the file does not fit in memory) 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 Iterating over a file uses it up: myfile = open("datafile.dat") for line_of_text in myfile: … process line_of_text This loop body will never be executed!

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

# Count the number of words in a text file and # make a list of all the words in the file num_words = 0 silly_file = open("silly.txt", "r") for line in silly_file: print line, silly_file.close() print "Total words in file: ", num_words

This is a silly file. Here is some silly more text This is a silly file. Here is some silly more text. And even another silly line. The fourth silly line.