Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)

Slides:



Advertisements
Similar presentations
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Advertisements

An Introduction to Python – Part III Dr. Nancy Warter-Perez.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
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.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
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.
PowerPoint: Level 2 State of Michigan Service Unit Administrative Staff.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
5 1 Data Files CGI/Perl Programming By Diane Zak.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
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])
Input and Output in python Josh DiCristo. How to output numbers str()  You can put any variable holding a number inside the parentheses and it will display.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Course A201: Introduction to Programming 09/09/2010.
Python I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
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.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Input, Output and Variables GCSE Computer Science – Python.
File Processing CMSC 120: Visualizing Information Lecture 4/15/08.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Formatting Output.
Python’s input and output
Computer Science 3 Hobart College
Formatting Output.
Computer Science 210 Computer Organization
Introduction to Objects
Class 9 Reading and writing to files chr, ord and Unicode
Computer Science 210 Computer Organization
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
Using files Taken from notes by Dr. Neil Moore
Chapter 3 Classes and Objects
The backslash is used to escape characters that are used in Python
Perl I/O Learning Objectives:
Project 1 General Approach.
Intro to PHP.
The keyboard is the standard input device.
Topics Introduction to File Input and Output
Lab 7: Filtering.
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.
F II 2. Simple Java Programs Objectives
Topics Introduction to File Input and Output
Formatting Output.
CS 1054 Introduction to Programming in Java
Introduction to Objects
Presentation transcript:

Files in Python Output techniques

Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)

Using print to output to a file You add one argument to the print function call. At the end of the argument list, put “file=“ followed by the name of the file object you have opened for output Example print(“hi”, a, c*23, end=“”, file= outfile) You can use anything in this print that you would in printing to the screen, end=, sep=, escaped characters, etc. Default end= and sep=, so gives a newline at the end of every print unless you give different value Note it says file=outfile, NOT file = “abc.txt”

Using write to output to a file write is a method, similar to the Text object in the graphics package it is called by the output file object (dot notation) It is allowed ONE and only one STRING argument, so you have to convert numbers to strings and concatenate strings together to make one argument Example outfile.write(“hi”+str(ct)+”\n”) Does NOT output a newline automatically, if you want one, you have to put one in the string