CSC 108H: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Lists Introduction to Computing Science and Programming I.
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Group practice in problem design and problem solving
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
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.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
CIT 590 Intro to Programming Files etc. Announcements From HW5 onwards (HW5, HW6,…) You can work alone. You can pick your own partner. You can also stick.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
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])
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review Prof. Katherine Gibson.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson Prof. Jeremy Dixon.
CSC 108H: Introduction to Computer Programming
MODULE 6 – RESOURCE LEVELING
Prof. Katherine Gibson Prof. Jeremy Dixon
CSc 120 Introduction to Computer Programing II
CSS 290: Video Games and Computer Programming
Topic: File Input/Output (I/O)
Writing & reading txt files with python 3
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
CSC 131: Introduction to Computer Science
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CSc 120 Introduction to Computer Programing II
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Python’s input and output
Variables, Expressions, and IO
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
CSC 108H: Introduction to Computer Programming
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
Python Lessons 13 & 14 Mr. Kalmes.
Teaching London Computing
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Lists in Python Creating lists.
Introduction to Python: Day Three
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
Introduction In today’s lesson we will look at: why Python?
Introduction to Statistics
15-110: Principles of Computing
CMSC201 Computer Science I for Majors Final Exam Information
Topics Introduction to File Input and Output
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Introduction to Computer Science
Python Lessons 13 & 14 Mr. Husch.
Topics Introduction to File Input and Output
CSC 221: Introduction to Programming Fall 2018
How to read from a file read, readline, reader
Introduction to Computer Science
Running & Testing Programs :: Translators
Presentation transcript:

CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki

Administration Midterm next week. 90 minutes. HS 610; 6:10pm. Old midterms posted online. Don't need to know any media stuff. Review in the second half of lecture. Office hours: Friday this week from 2-4. Monday and Wednesday next week from 12-2. June 23 2011

Files. So far we've seen some basic file stuff. Media opens files Assignment two has a method that opens a file. June 23 2011

Files as types. Python has a type used to deal with files. There are four main things we want to do with files: Figure out how to open them. Figure out how to read them. Figure out how to write to them. Figure out how to close them. June 23 2011

Opening files. Can hardcode the filename in the code. Like done in assignment 2. Can ask the user for a file name using raw_input() Some modules have their own builtin functions for opening files. media has choose_file() which opens a dialog window. June 23 2011

Opening files. Once we have a filename we can call open: open(filename, 'r') – for reading (this is the default mode). open(filename, 'w') – for writing (erases the contents of a file). open(filename, 'a') – for appending (keeps the contents of the file). This function returns a new object, a file object. June 23 2011

Reading Files. The most basic way is the read the whole file into a string: filename.read() - returns a string that is the contents of the entire file. Not recommended for big files. Can read a single line of the file. filename.readline() - reads a line of the filename. A subsequent call the readline() will read the next line of the file, the first line is lost. June 23 2011

Reading Files. Can read a fixed number of characters. filename.read(10) – will read 10 characters. If you call it again, it will start reading from the place after the characters that it has read. Can read the file a line at a time. for line in filename: print line Note that the string split method is often very useful. June 23 2011

Writing to Files. Write to files using: filename.write(“This is a string”) Multiple writes are concatenated. Need to open a file in append or write mode to write to it. Append mode will add the strings to the end of the file. June 23 2011

Closing Files. Close a file with: filename.close() Generally a good idea. Frees up system resources. June 23 2011

Midterm Review and Assignment Questions. Done in shell. June 23 2011