Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.

Slides:



Advertisements
Similar presentations
Introduction to Computing Using Python Text Data, File I/O, and Exceptions  Strings, revisited  Formatted output  File Input/Output  Errors and Exceptions.
Advertisements

Introduction to Computing Using Python Text Data, File I/O, and Exceptions  Strings, revisited  Formatted output  File Input/Output  Errors and Exceptions.
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.
File System Interface CSCI 444/544 Operating Systems Fall 2008.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
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 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.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
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.
Topics This week: File input and output Python Programming, 2/e 1.
Microsoft Office Illustrated Fundamentals Unit B: Understanding File Management.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
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.
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.
Course A201: Introduction to Programming 12/9/2010.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
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.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Files Tutor: You will need ….
CIS 234: File Input & Output introduction. Data Storage Is Fundamental computers need to store data to work with it memory (RAM) is fast but transient.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
Chapter 11 File Systems and Directories. 2 File Systems (Chapter 11.1) File: 1. A named collection of related data. 2.smallest amount of information that.
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 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.
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 3 High-level Programming with Python Part III: Files and Directories Reference:
Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell.
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.
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()
File Processing CMSC 120: Visualizing Information Lecture 4/15/08.
File Processing Upsorn Praphamontripong CS 1110
Topic: File Input/Output (I/O)
COMP Streams and File I/O
File I/O File input/output Iterate through a file using for
File Writing Upsorn Praphamontripong CS 1110
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CSc 120 Introduction to Computer Programing II
Python’s input and output
Exceptions and File Input
CSC 108H: Introduction to Computer Programming
Ruth Anderson UW CSE 160 Winter 2016
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Spring 2018
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
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
File I/O File input/output Iterate through a file using for
CS 1111 Introduction to Programming Fall 2018
Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)
Files Handling In today’s lesson we will look at:
Strings, Lists, and Files
15-110: Principles of Computing
Topics Introduction to File Input and Output
Microsoft Office Illustrated Fundamentals
Topics Introduction to File Input and Output
CS 1111 Introduction to Programming Spring 2019
Presentation transcript:

Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file

Introduction to Computing Using Python What is a file?  A file is a collection of data  Files are usually stored in non-volatile memory (they don’t disappear when the power is turned off)  A common type of file is a text file  A text file is a sequence of characters, e.g.  Computer programs  Word processing documents  A binary file may contain data in some non-text format  Music, images and video are common  Instrument readings

Introduction to Computing Using Python Why are files?  Data may be stored in a file because  It is bigger than available RAM  It requires long-term storage  It can be easily duplicated  It can be easily transported  It is cheap  Files are usually stored in devices at the bottom of a memory hierarchy  High end: fast, small, expensive, requires power (volatile)  Low end: big, slow, cheap, stable (non-volatile)

Introduction to Computing Using Python Why are files?

Introduction to Computing Using Python Files and the file system / / Applications Users bin var Firefox.app Mail.app Shared messi poem.txt image.jpg Contents MacOS Mail Canon The file system is the OS component that organizes files and provides a way to create, access, and modify files Files are organized into a tree structure root folder / / Applications Users bin var Firefox.app Mail.app Shared messi Contents MacOS Canon folders (or directories) regular files text filebinary file While every file and folder has a name, it is the file pathname that identifies the file poem.txt Absolute path (from root) /var/poem.txt /Users/messi/poem.txt /Applications/Mail.app/ Relative (to current working directory) path messi/poem.txt messi/image.jpg Shared

Introduction to Computing Using Python Use the built-in function open(arg1, arg2) to open a file The first argument is the path, either absolute or relative to the current working directory The second (optional) argument is the file mode >>> inFile = ('thisLandIsYourLand', 'r') 1.Open the file 2.Read from and/or write to the file 3.Close the file File mode ' r ' is used to open a file for reading, ' w ' for writing open() returns an object of type “file.” A file object supports several methods, including close() How to use a file

Introduction to Computing Using Python Open file mode ModeDescription r Reading (default) w Writing (if file exists, content is wiped) a Append (if file exists, writes are appended) r+ Reading and Writing t Text (default) b Binary The file mode defines how the file will be accessed >>> infile = open('example.txt', 'rt') >>> infile = open('example.txt', 'r') >>> infile = open('example.txt', 't') >>> infile = open('example.txt') >>> infile = open('example.txt', 'rt') >>> infile = open('example.txt', 'r') >>> infile = open('example.txt', 't') >>> infile = open('example.txt') These are all equivalent

Introduction to Computing Using Python File methods UsageDescription infile.read(n) Read n characters starting from cursor; if fewer than n characters remain, read until the end of file infile.read() Read starting from cursor up to the end of the file infile.readline() Read starting from cursor up to, and including, the end of line character infile.readlines() Read starting from cursor up to the end of the file and return a list of lines outfile.write(s) Write string s to file outfile starting from cursor infile.close(n) Close file infile There are several Python methods for reading from and writing to files read() returns the entire file as a string readline() returns one line (up to the end-of-line character) as a string readlines() returns the entire file as a list of strings (one line = one string) write() returns the number of characters written

Introduction to Computing Using Python Reading a file >>> infile = open('example.txt') >>> >>> infile = open('example.txt') >>> The 3 lines in this file end with the new line character.\n \n There is a blank line above this line.\n The 3 lines in this file end with the new line character.\n \n There is a blank line above this line.\n ⌃⌃⌃ ⌃ ⌃ >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> infile.readline() 'lines in this file end with the new line character.\n' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> infile.readline() 'lines in this file end with the new line character.\n' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> infile.readline() 'lines in this file end with the new line character.\n' >>> infile.read() '\nThere is a blank line above this line.\n' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> infile.readline() 'lines in this file end with the new line character.\n' >>> infile.read() '\nThere is a blank line above this line.\n' >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> infile.readline() 'lines in this file end with the new line character.\n' >>> infile.read() '\nThere is a blank line above this line.\n' >>> infile.close() >>> >>> infile = open('example.txt') >>> infile.read(1) 'T' >>> infile.read(5) 'he 3 ' >>> infile.readline() 'lines in this file end with the new line character.\n' >>> infile.read() '\nThere is a blank line above this line.\n' >>> infile.close() >>> example.txt When the file is opened, a cursor is associated with the opened file The initial position of the cursor is: at the beginning of the file, if file mode is r at the end of the file, if file mode is a or w

Introduction to Computing Using Python Patterns for reading a text file def numChars(filename): '''return the number of characters in file filename''' infile = open(filename, 'r') content = infile.read() infile.close() return len(content) def numChars(filename): '''return the number of characters in file filename''' infile = open(filename, 'r') content = infile.read() infile.close() return len(content) Common patterns for reading a file: 1.Read the file content into a string 2.Read the file content into a list of words 3.Read the file content into a list of lines def numWords(filename): '''return the number of words in file filename''' infile = open(filename) content = infile.read() infile.close() wordList = content.split() return len(wordList) def numWords(filename): '''return the number of words in file filename''' infile = open(filename) content = infile.read() infile.close() wordList = content.split() return len(wordList) def numLines(filename): '''return the number of lines in filename''' infile = open(filename, 'r’) lineList = infile.readlines() infile.close() return len(lineList) def numLines(filename): '''return the number of lines in filename''' infile = open(filename, 'r’) lineList = infile.readlines() infile.close() return len(lineList)

T2341 T234 1 T2341 T234 1 This is the first line This is the first line This is the first line. Still the first line…\n This is the first line. Still the first line…\n This is the first line. Still the first line…\n 2 Now we are in the second line.\n This is the first line. Still the first line…\n 2 Now we are in the second line.\n This is the first line. Still the first line…\n 2 Now we are in the second line.\n 3 Non string value like 5 must be converted first.\n 4 1 This is the first line. Still the first line…\n 2 Now we are in the second line.\n 3 Non string value like 5 must be converted first.\n 4 This is the first line. Still the first line…\n Now we are in the second line.\n Non string value like 5 must be converted first.\n This is the first line. Still the first line…\n Now we are in the second line.\n Non string value like 5 must be converted first.\n Introduction to Computing Using Python Writing to a text file ⌃⌃ ⌃ ⌃ ⌃ >>> outfile = open('test.txt', 'w') >>> >>> outfile = open('test.txt', 'w') >>> test.txt ⌃⌃ >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> outfile.write('Now we are in the second line.\n') 31 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> outfile.write('Now we are in the second line.\n') 31 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> outfile.write('Now we are in the second line.\n') 31 >>> outfile.write('Non string value like '+str(5)+' must be converted first.\n') 49 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> outfile.write('Now we are in the second line.\n') 31 >>> outfile.write('Non string value like '+str(5)+' must be converted first.\n') 49 >>> >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> outfile.write('Now we are in the second line.\n') 31 >>> outfile.write('Non string value like '+str(5)+' must be converted first.\n') 49 >>> outfile.write('Non string value like {} must be converted first.\n'.format(5)) 49 >>> outfile.close() >>> outfile = open('test.txt', 'w') >>> outfile.write('T') 1 >>> outfile.write('his is the first line.') 22 >>> outfile.write(' Still the first line...\n') 25 >>> outfile.write('Now we are in the second line.\n') 31 >>> outfile.write('Non string value like '+str(5)+' must be converted first.\n') 49 >>> outfile.write('Non string value like {} must be converted first.\n'.format(5)) 49 >>> outfile.close()