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
ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Advertisements

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.
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 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 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 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
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.
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.
Python programs How can I run a program? Input and output.
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
General Programming Introduction to Computing Science and Programming I.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
File I/O ifstreams and ofstreams Sections 11.1 &
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
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:
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(),
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.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
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.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
 Prepared by: Eng. Maryam Adel Abdel-Hady
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python File Handling. Python language provides numerous built-in functions Some of the functions widely used for standard input and output operations.
Python Basics.
Introduction To Files In Python
MATLAB – More Script Files
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Computer Programming Fundamentals
(optional - but then again, all of these are optional)
(optional - but then again, all of these are optional)‏
Engineering Innovation Center
Line Continuation, Output Formatting, and Decision Structures
Topics Introduction to File Input and Output
Introduction To Files In Python
Introduction To Files In Python
Introduction to Programming
Topics Introduction to File Input and Output
Introduction to Computer Science
Chopin’s Nocturne.
Input and Output Python3 Beginner #3.
Hint idea 2 Split into shorter tasks like this.
Introduction to Programming
Topics Introduction to File Input and Output
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? Many reasons: -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/231/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: print line inputFile.close() print "Completed reading of file", inputFileName,

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

James Tam An Alternate Method Of Getting Input Command line arguments: inputs given to a program as it’s run. The executable program can be found in UNIX under: /home/231/examples/files/command_line1.py Example: “ python command_line.py first_input 2ndInput ” > import sys def main (): first = sys.argv[0] # Name of program (command_line.py) second = sys.argv[1] # First input (first_input) third = sys.argv[2] # Second input (2ndInput) main ()

James Tam An Alternate Method Of Getting Input (2) The executable program can be found in UNIX under: /home/231/examples/files/command_line2.py > import sys def main (): arguments = sys.argv[0:] for argument in arguments: print argument main ()

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/231/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 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: if (line[0] == "A"): gpa = 4 elif (line[0] == "B"): gpa = 3 elif (line[0] == "C"): gpa = 2 elif (line[0] == "D"): gpa = 1 elif (line[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 Another Example Reading From A File Into A String A complete version of this program can be found in UNIX under: /home/231/examples/files/file_list.py inputFile = open ("input.txt", "r") for line in inputFile: i = 0 for ch in line: print i, ch, i = i + 1 print

James Tam Building An Arbitrary Sized List By Reading From File A complete version of this program can be found in UNIX under: /home/231/examples/files/file_list2.py inputFile = open ("input2.txt", "r") myList = [] for line in inputFile: myList.append(line) inputFile.close()

James Tam Building An Arbitrary Sized List By Reading From File (2) row = 0 for line in myList: if (row < 10): print row, line, else: temp = row + 55 ch = chr(temp) print ch, line, row = row + 1

James Tam You Should Now Know How to open a file for reading How to open a file a file for writing 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 What is a command line argument How to read and use command line arguments How to read from a file of arbitrary size How to build an arbitrary sized list by reading the information from a file