James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.

Slides:



Advertisements
Similar presentations
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
Advertisements

J. Michael Moore Text Files CSCE 110 From James Tam’s material.
Slide 1 Introduction To Files In Python In this section of notes you will learn how to read from and write to text files.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Arrays / Lists In this section of notes you will be introduced to new type of variable that consists of other types.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
Making Decisions In Python
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to text files in Pascal.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python programs How can I run a program? Input and output.
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Creating your first C++ program
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
File Handling and Control Break Logic. Objectives In this chapter, you will learn about: Computer files Writing a program that reads from and/or writes.
File I/O ifstreams and ofstreams Sections 11.1 &
Python – May 11 Briefing Course overview Introduction to the language Lab.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
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,
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
INPUT & VARIABLES.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Python File Handling. Python language provides numerous built-in functions Some of the functions widely used for standard input and output operations.
Introduction To Files In Python
Topic: File Input/Output (I/O)
MATLAB – More Script Files
Introduction to Python
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Line Continuation, Output Formatting, and Decision Structures
(optional - but then again, all of these are optional)
(optional - but then again, all of these are optional)‏
Design & Technology Grade 7 Python
Engineering Innovation Center
Line Continuation, Output Formatting, and Decision Structures
Introduction To Files In Python
Class Examples.
Introduction To Files In Python
This presentation document has been prepared by Vault Intelligence Limited (“Vault") and is intended for off line demonstration, presentation and educational.
Stata Basic Course Lab 2.
Topics Introduction to File Input and Output
Introduction to Computer Science
Chopin’s Nocturne.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Python 10 Mr. Husch.
Hint idea 2 Split into shorter tasks like this.
Challenge Guide Grade Code Type Slides
Presentation transcript:

James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.

James Tam Why Bother With Files? Too much information to input all at once The information must be persistent (RAM is volatile) Data entry of information is easier via a text editor rather than through the computer program that you write. Etc.

James Tam What You Need In Order To Read Information From A File 1.Open the file and associate the file with a file variable 2.A command to read the information

James Tam 1.Opening Files Prepares the file for reading: A.Links the file variable with the physical file (references to the file variable are references to the physical file). B.Positions the file pointer at the start of the file. Format: 1 = open (, “r”) Example: (Constant file name) inputFile = open ("data.txt ", "r") OR (Variable file name: entered by user at runtime) filename = raw_input ("Enter name of input file: ") inputFile = open (filename, "r") 1 Assumes that the file is in the same directory/folder as the Python program.

James Tam B.Positioning The File Pointer A B C B : letters.txt

James Tam 2.Reading Information From Files Typically reading is done within the body of a loop Format: for in : Example: for line in inputFile: print line

James Tam Reading From Files: Putting It All Together A complete version of this program can be found in UNIX under /home/courses/217/examples/files/grades.py: inputFileName = raw_input ("Enter name of input file: ") inputFile = open (inputFileName, "r") print "Opening file", inputFileName, " for reading." # While we haven't read past the end of the file continue reading from # it. for line in inputFile: letter = line print letter inputFile.close() print "Completed reading of file", inputFileName,

James Tam View Of File Example In UNIX home 217 examples files letters.txt grades.py / courses

James Tam What You Need To Write Information To A File 1.Open the file and associate the file with a file variable 2.A command to write the information

James Tam 1.Opening The File Format: = open (, “w”) Example: (Constant file name) outputFile = open (“gpa.txt”, "w") (Variable file name: entered by user at runtime) outputFileName = raw_input ("Enter the name of the output file to record the GPA's to: ") outputFile = open (outputFileName, "w")

James Tam 3.Writing To A File Format: outputFile.write (temp) Example: # Assume that temp contains a string of characters. outputFile.write (temp )

James Tam Writing To A File: Putting It All Together A complete version of this program can be found in UNIX under: /home/courses/217/examples/files/grades2.py inputFileName = raw_input ("Enter the name of input file to read the grades from: ") outputFileName = raw_input ("Enter the name of the output file to record the GPA's to: ") # Open file for reading, confirm file with user. inputFile = open (inputFileName, "r") outputFile = open (outputFileName, "w") # Update user on what is happening. print "Opening file", inputFileName, " for reading." print "Opening file", outputFileName, " for writing."

James Tam Writing To A File: Putting It All Together (2) gpa = 0 for line in inputFile: letter = line if (letter[0] == "A"): gpa = 4 elif (letter[0] == "B"): gpa = 3 elif (letter[0] == "C"): gpa = 2 elif (letter[0] == "D"): gpa = 1 elif (letter[0] == "F"): gpa = 0 else: gpa = -1

James Tam Writing To A File: Putting It All Together (3) temp = str (gpa) temp = temp + '\n' print letter[0], '\t', gpa outputFile.write (temp) inputFile.close () outputFile.close () print "Completed reading of file", inputFileName, print "Completed writing to file", outputFileName,

James Tam You Should Now Know How to open a file for reading How to open a file a file for writing How to read from and write to a file The details of how information is read from and written to a file How to close a file and why it is good practice to do this explicitly