File I/O Dr. Nancy Warter-Perez. Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via

Slides:



Advertisements
Similar presentations
Lab III – Linux at UMBC.
Advertisements

The INFILE Statement Reading files into SAS from an outside source: A Very Useful Tool!
Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
An Introduction to Python – Part III Dr. Nancy Warter-Perez.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
PERL P ractical E xtraction R ecording L anguage.
Chapter 5: Loops and Files.
An Introduction to Python – Part III Dr. Nancy Warter-Perez May 1, 2007.
An introduction to Python and its use in Bioinformatics Csc 487/687 Computing for Bioinformatics Fall 2005.
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.
An Introduction to Python – Part III Dr. Nancy Warter-Perez.
Topics This week: File input and output Python Programming, 2/e 1.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
Python programs How can I run a program? Input and output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
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.
1 CS161 Introduction to Computer Science Topic #13.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Introduction to Java Files. Text Files A sequential collection of data stored on a permanent storage device Hard drive USB memory CD/DVD Has a name and.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
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.
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,
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Files Tutor: You will need ….
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Loops and Files. 5.1 The Increment and Decrement Operators.
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(),
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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])
Chapter 2 Getting Data into SAS Directly enter data into SAS data sets –use the ViewTable window. You can define columns (variables) with the Column Attributes.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Files in Python Opening and Closing. Big Picture To use a file in a programming language – You have to open the file – Then you process the data in the.
File Processing Upsorn Praphamontripong CS 1110
Chapter 14: Sequential Access Files
Reading and writing files
Creates the file on disk and opens it for writing
Module 5 Working with Data
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
Introduction to Scripting
Managing results files
Data File Import / Export
Python I/O.
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
File I/O File input/output Iterate through a file using for
Creates the file on disk and opens it for writing
File I/O File input/output Iterate through a file using for
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Fundamentals of Data Structures
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
CS 1111 Introduction to Programming Fall 2018
Topics Introduction to File Input and Output
File Input and Output.
CS 1111 Introduction to Programming Spring 2019
Presentation transcript:

File I/O Dr. Nancy Warter-Perez

Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via Subject template for HWx: Chem434 HWx or Binf400 HWx Example for HW1P: Chem434 HW1P or Binf400 HW1P sample output (screen shot(s)) to show that your program is working properly

Introduction to Python – Part II3 Homework Due Dates Programming Homework Dues Dates are somewhat flexible because of the diverse programming skills of students in the class. Always submit your homework on the due date. If the program is not working, also submit an explanation of where the problem is. If the program is working, submit screen shots that demonstrate your program is working properly. You have up to one week to resubmit your homework (with no penalty) after the due date. Follow the same guidelines (i.e., if working submit screen shots and if not working, submit explanation (and screen shots if possible).

Introduction to Python – Part II4 File Inputs and Outputs (I/O) Data can be read directly from a file and results can be written directly to a file. First you need to open the file: Output Ex: outfile = open("out.txt", 'w') 'w' indicates that the file will be written to by the script (i.e., it is an output file) The file out.txt will be created by the script in the same directory as your script The variable outfile will allow you to refer to the out.txt file in your program. You can use any variable name you want.

Introduction to Python – Part II5 File Inputs and Outputs (I/O) Another example of opening a file: Input Ex: infile = open("D:\\Docs\\test.txt", 'r') 'r' indicates that the file will be read by the script (i.e., it is and input file) The file test.txt should already exist in the folder specified Notice that here the exact path of where to find the file is given (" D:\\Docs\\test.txt") The variable infile will allow you to refer to test.txt file in your program. You can use any variable name that you want.

Introduction to Python – Part II6 File Inputs and Outputs (I/O) Second, after opening the file you can either read or write to it. Input Ex: s = infile.read() The entire contents of the input file (test.txt) will be read in as a string and stored in s. Refer to slide 9 for more input file operations. Output Ex: outfile.write(str) This will write the contents of the string variable (str) into the file (out.txt) Refer to slide 10 for more output file operations.

Introduction to Python – Part II7 File Inputs and Outputs (I/O) Third, after you are done with the file, close it so that other applications can access the file. Input Ex: infile.close() Output Ex: outfile.close()

Introduction to Python – Part II8 Common input file operations OperationInterpretation input = open ('file', 'r')open input file S = input.read()read entire file into string S S = input.read(N)Read N bytes (N>= 1) S = input.readline()Read next line L = input.readlines()Read entire file into list of line strings

Introduction to Python – Part II9 Common output file operations OperationInterpretation output = open('file', 'w')create output file output.write(S)Write string S into file output.writelines(L)Write all line strings in list L into file output.close()Manual close (good habit)

Introduction to Python – Part II10 Example Write a script to read in a string of integer numbers from a file, convert each character (‘0’ to ‘9’) to an integer, and store it into an output file

Introduction to Python – Part II11 Example Python Script # Example file I/O script # Script will read in an input string of numbers from an input file, # convert each character to an integer and write the converted # values to an output file. # # Written by: Prof. Nancy Warter-Perez # Date created: May 5, 2009 infile = open("input.txt", "r") # before running the script, create input.txt with a string of characters outfile = open("output.txt", "w") # will be created by the program. s = infile.read() #read in entire file into s (note, there are other options for reading files) infile.close() #done reading the file for c in s: outfile.write("%s," % int(c)) #writes each value to the output file separated by comma outfile.close()

Introduction to Python – Part II12 Example input and output files Contents of input.txt before (and after) running the script Contents of output.csv after running the script 1,2,3,4,5,6,7,8,9,8,7,6,5,4,5,6,7,8,9,

Introduction to Python – Part II13 Plot using Excel In Excel, open the output.txt file (as a text file) Choose delimited data type and select comma as a delimiter Select the row of data Select the output graph format Example: