Last Week Looping though lists Looping using range While loops.

Slides:



Advertisements
Similar presentations
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
Advertisements

Computer Science 111 Fundamentals of Programming I Files.
File input and output if-then-else Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Why use Files? Your Python Program External File (secondary storage) Write to file (Save) Read from file (Load) …so we can have access to ‘stored’ data.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Files Introduction to Computing Science and Programming I.
Chapter 10.
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.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Introduction to Computational Linguistics Programming I.
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.
For. for loop The for loop in Python is more like a foreach iterative-type loop in a shell scripting language than a traditional for conditional loop.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 18 Using text files to share data with other programs 5/07/09 Python Mini-Course:
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.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Files Tutor: You will need ….
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Last Week Lists List methods Nested Lists Looping through lists using for loops.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
File input and output and conditionals Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
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.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures.
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.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
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.
Prof. Katherine Gibson Prof. Jeremy Dixon
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Interfacing with a database
Chapter 4 Strings & Tuples
CSc 120 Introduction to Computer Programing II
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.
Python’s input and output
Ruth Anderson UW CSE 160 Winter 2016
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
Python I/O.
Ruth Anderson UW CSE 160 Spring 2018
Topics Introduction to File Input and Output
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Files
File Handling.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Fundamentals of Data Structures
Using Text Files in Python
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
More for loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
How to save information in files open, write, close
Files Handling In today’s lesson we will look at:
CMSC201 Computer Science I for Majors Lecture 13 – File I/O
CS190/295 Programming in Python for Life Sciences: Lecture 3
Topics Introduction to File Input and Output
Topics Introduction to File Input and Output
Programming Techniques :: File Handling
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

Last Week Looping though lists Looping using range While loops

This Week Special characters in strings Files – what are they? Opening and closing files Reading and writing to files Comma Separated Files

Invisible Characters How do we indicate a tab, newline or ‘ or “ inside a string? \ttab\”double quote \nnew line \’single quote\\backslash These are called escape sequences. print(“This string has a \n newline and a \t tab.” This string has a newline and a tab.

Files Files – what are they? –Can think of as a collection of stored information –Computer thinks as a sequence of bits possible arranged in characters Files have names: –my_python.py, homework.txt, etc. What can we do with a file? –Read from a file and write to a file

Opening Files How do we open a file in Python? import io myfile = open(‘story.txt’, ‘r’) Open is the Python function Story.txt is the name of the file to be opened myfile is a variable that is assigned the file object (also called stream or reader). ‘r’ is a string indicating that we will do with the file (read, write, append)

Using Files After we are finished with a file we must close it: myfile.close() When we write to a file, we have two choices: Write: myfile = open(‘filename’, ‘w’) Append: myfile = open(‘filename’, ‘a’) write replaces the file filename append appends to the file filename

Reading Files There are many ways to read from a file. 1.Using a for loop: myfile = open(‘filename’, ‘r’) for line in myfile: 2.Read the whole file at once into a list of strings: myfile = open(‘filename’, ‘r’) list_of_lines = myfile.readlines()

Reading Files 3.Read the entire file into a string: myfile = open(‘filename’, ‘r’) s = myfile.read() 4.Read a specific number of characters: myfile = open(‘filename’, ‘r’) s = myfile.read(10) reads 10 characters s = myfile.read(10) reads then next 10 characters

Reading Files 5.Read one line at a time: myfile = open(‘filename’, ‘r’) line = myfile.readline() reads a line line = myfile.readline() reads the next line

End Of File (EOF) How do we know when we reach the end of the file? This method automatically recognizes EOF. for line in myfile: Here we have to check for the end of file: line = myfile.readline() reads the next line and s = myfile.read(10) reads 10 characters The EOF character is the empty string “”.

Writing to a file First open the file for writing or appending: myfile = open(‘story.txt’, ‘w’) start the story myfile = open(‘story.txt’, ‘a’) continue the story Then write to the file: myfile.write(‘Once upon a time…’) myfile.write(‘The end.’) Then close the file: myfile.close()

Reading from a CSV file Often we have comma-separated values data files: First Name, Last Name, Utorid Anna, Bretscher, bretsche Joe, Johnson, johnsonj Sally, Jordan, jordansa Why do we like CSV files? –Spreadsheet applications like excel understand it –Many other programming languages also understand them –Easy to generate ourselves

Reading from a CSV file How do we read comma-separated values data files: import io import csv csv_file = open(‘csv_filename.csv’, ‘r’) reader = csv.reader(csv_file) … for line in reader: # read like an ordinary file # but line is a list