Python Working with files. May 4, 2017

Similar documents
File Input/Output in Python. October 9, 2017

ENGG1811 Computing for Engineers Week 9A: File handling

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Why NumPy / SciPy? NumPy / SciPy / Matplotlib. A Tour of NumPy. Initializing a NumPy array

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Unit 4. Input/Output Functions

Should you know scanf and printf?

Programming in Python 3

Fundamentals of Python: First Programs. Chapter 4: Strings and Text Files

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ENGR 1181 MATLAB 05: Input and Output

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

2.1 Indefinite Loops. while <condition>: <body> rabbits = 3 while rabbits > 0: print rabbits rabbits -= 1

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

C mini reference. 5 Binary numbers 12

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

Python Programming Exercises 1

Java Bytecode (binary file)

More about Binary 9/6/2016

Advanced C Programming Topics

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

COMP322 - Introduction to C++

ENGR 102 Engineering Lab I - Computation

DATA STRUCTURE AND ALGORITHM USING PYTHON

Strings Additions File I/O. Strings and I/O. K. Cooper 1. 1 Department of Mathematics. Washington State University. Strings

MATLAB Introduction to MATLAB Programming

Chapter 7. Basic Types

The C++ Language. Arizona State University 1

Full file at

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 11 Customizing I/O

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lesson 2 Characteristics of Good Code Writing (* acknowledgements to Dr. G. Spinelli, New Mexico Tech, for a substantial portion of this lesson)

bash, part 3 Chris GauthierDickey

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

PYTHON MOCK TEST PYTHON MOCK TEST III

Objectives. In this chapter, you will:

Binghamton University. CS-220 Spring Includes & Streams

Input/Output Streams: Customizing

Chapter 11 Customizing I/O

Chapter 11 Customizing I/O

AWK - PRETTY PRINTING

IT 374 C# and Applications/ IT695 C# Data Structures

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Announcements COMP 141. Writing to a File. Reading From a File 10/18/2017. Reading/Writing from/to Files

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Reserved Words and Identifiers

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Phys Techniques of Radio Astronomy Part 1: Python Programming LECTURE 3

Part III Appendices 165

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

File Storage Techniques in LabVIEW

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Input / Output Functions

CS 115 Lecture 4. More Python; testing software. Neil Moore

PySoundFile Documentation Release 0.6.0

Description Syntax Remarks and examples Conformability Diagnostics Also see

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Starting chapter 5. l First open file, and say purpose read or write inputfile = open('mydata.txt', 'r') outputfile = open('myresults.

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Access to Delimited Text Files

Software Design & Programming I

UNIVERSAL SERIAL INTERFACE

Introduction to C# Applications

Introduction to Python Programming

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Introduction to Scientific Computing Lecture 1

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Chapter 2: Introduction to C++

System Software Experiment 1 Lecture 7

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 5. Essential skills for bioinformatics: Unix/Linux

CSC Advanced Scientific Computing, Fall Numpy

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Text Input and Conditionals

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

write (unit=*,fmt=*) i =, i! will print: i = 3

STSCI Python Introduction. Class URL

Course organization. Course introduction ( Week 1)

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Integer Data Types. Data Type. Data Types. int, short int, long int

1 Characters & Strings in Fortran

Topic 6: A Quick Intro To C

The C Programming Language Guide for the Robot Course work Module

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

PHY224 Practical Physics I. Lecture 2

Program Fundamentals

Midterm spring. CSC228H University of Toronto

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

Python for Non-programmers

COLLEGE OF ENGINEERING, NASHIK-4

Beginning C Programming for Engineers

Transcription:

Python Working with files May 4, 2017

So far, everything we have done in Python was using in-memory operations. After closing the Python interpreter or after the script was done, all our input and output was gone. In real life applications we want to work with files stored on the computers hard drive.

Example situations were files are necessary: A collaborator sends you raw data via email. A program you are using reads input data from a file. When running the program multiple times, you don t want to recreate the data every time it is needed, but store it in a file and reuse it. Another program produces lots of output that you want to analyze using your own program. You want to keep intermediate calculations for further processing.

Scientific data is typically stored in plain text files. This means human readable files, with no internal format information. Although there are sometimes special binary based formats. In Python to save or load data you need to use a special file handle object. The built-in open() function will return a file handle object. It takes a path to the file you want to open as an argument. So let s say we want to open a file called data.txt and get a file handle f to operate on the file. This can be accomplished using the following code: f = open ( data. txt )

The open() function implicitly performed the following actions: checks if data.txt exists, throws an exception otherwise creates the file handle sets the cursor position to the start of the file, pos = 0 It did not read anything from the file, nor did it write to it. The file is also not closed after this function call returns. All these actions must be done separately.

File methods Here is a list of the most important file methods. f. read ( n= -1) # Reads n byte from the file. If n not set or is -1 # the entire rest of the file is read. f. readline () # Reads in the next full line and returns a string # which includes the newline character and the end. f. readlines () # Reads the remaining lines into a list of strings. f. seek ( pos ) # Moves the file cursor to a specific position. f. tell () # Returns the current position of the cursor. f. write ( s) # Inserts the string s at the current position. f. flush () # Performs all pending write operations, making # sure that they are really on disk. f. close () # Closes the file. No more reading or writing possible # After this call.

Example Let say we have a file matrix.txt with the following content: 1,4,15,9 0,11,7,3 2,8,12,13 14,5,10,6 To read this file into Python we could use the following code snippet: f = open ( matrix. txt ) matrix = [] for line in f. readlines (): row = [ int (x) for x in line. split (, )] matrix. append ( row ) f. close () Another version: matrix = [] with open ( matrix. txt ) as f: for line in f. readlines (): row = [ int (x) for x in line. split (, )] matrix. append ( row )

File modes Files are opened in one of multiple modes. The mode determines the methods that can be used on the file handle. Invalid methods are still callable, but will throw an exception. So far we only used the default read-only mode, passing no additional argument to the open() function. If we wanted to open a file for writing, we would use the following code: f = open ( data. txt, w )

File modes Here is a list modes available: r # read only mode, starting pos = 0 w # write mode, creates file if it does not exist # overwrites existing files ( CAUTION!), starting pos = 0 a # append mode, like write mode, but pos is at the # end of the file + # update mode, opens for both reading and writing, # but does not delete current content, starting pos = 0 These modes are the same for other programming languages (especially C) as well.

File mode example Let s look at the matrix.txt example again. # old matrix. txt # 1,4,15,9 # 0,11,7,3 # 2,8,12,13 # 14,5,10,6 f = open ( matrix. txt, r+ ) # open in read and write mode orig = f. read () # read entire file into a single string f. seek (0) # rewind file ( go back to the start ) f. write ( 0,0,0,0\ n ) # write new line, overwriting existing one f. write ( orig ) # write original contents back after added line f. write ( 1,1,1,1\ n ) # add another line to the end f. close () # close file, we re done here # new matrix. txt # 0,0,0,0 # 1,4,15,9 # 0,11,7,3 # 2,8,12,13 # 14,5,10,6 # 1,1,1,1

numpy The numpy module comes with special functions to read and write data text files. To read a data file, where every row has the same amount of columns you could use numpy s loadtxt() function. from numpy import loadtxt matrix = loadtxt ( matrix. txt ) The full list of options for the loadtxt() function looks like this: loadtxt ( fname, dtype =< type float >, comments = #, delimiter =None, converters =None, skiprows =0, usecols =None, unpack = False, ndmin =0)

loadtxt options The most important loadtxt() options are the following: fname # filename or str dtype # data type of resulting array. default value : float comments # str, optional, character used for comments in the file # default value : # delimiter # str, optional, String used to separate values # default value is a whitespace skiprows # int, optional, skips the number of rows given usecols # sequence, optional read only given columns # (1, 4, 5) reads second, 5th and 6th column / line unpack # bool, optional, default : False # if True, the returned array is transposed

numpy savetxt Saving data to a file works just as easy as reading them. numpy provides the savetxt() function to accomplish this. from numpy import savetxt matrix = [ [1, 4, 15, 9], [0, 11, 7, 3], [2, 8, 12, 13], [14, 5, 10, 6] ] savetxt ( matrix. txt, matrix ) Again you can customize how the data is saved using the optional parameters of savetxt(): savetxt ( fname, X, fmt = %.18 e, delimiter =, newline = \n, header =, footer =, comments = # )

savetxt options The most important savetxt() options are the following: fname # filename or str X # array, data to be saved # use transpose ([ x, y]) to save two arrays in two columns fmt # str or sequence of strs, optional # defines the output format for the data comments # str, optional, character used for comments in the file # default value : # delimiter # str, optional, String used to separate values # default value is a whitespace header # str, optional # inserted before data footer # str, optional # inserted at the end of the file

savetxt - format parameter Here I want to explain the fmt parameter in more detail. The full string looks something like this: (%[ flag ] width [. precision ] specifier ) possible flags: - # left justify + # Forces to preceed result with the sign (+ or -) 0 # Left pad the number with zeroes instead of space width is the minimum number of characters to be printed, longer values will not be truncated to width. Precision: for integer specifiers (eg. d, i, o, x), the minimum number of digits for e, E and f, number of digits after decimal points for g and G, number of significant digits for s, maximum number of characters

savetxt - format parameter Specifiers: c # character d or i # signed decimal integer e or E # scientific notation (1 e6 or 10E -9) f # decimal floating point g or G # use the shorter e, E or f o # signed octal s # string of characters u # unsigned decimal integer x, X # unsigned hexadecimal integer