Python: File I/O - Motivation Up to this point, all input ...

Python: File I/O - Motivation ? Up to this point, all input to your programs has been via the keyboard

? But suppose you had lots of values you wanted to process ? You could enter them all from the keyboard, but the more you do the more

prone you'd probably be to make errors, and if processing had to occur on all of the values at the same time, you wouldn't be able to enter some at one time and others at some other time. ? By the same token, what if a program generated lots of output. ? You could have it all printed to the screen at once, but to examine it you might need to scroll thru many many screenfuls ? Possibly there would be so much you would lose some because you couldn't scroll back that far ? Files are a solution to the above problems ? You're already familiar with files ? Now, we're going to use files to hold input to a program, and use files to hold output from a program ? Input files can be created over a period of time, can be spell-checked, etc. ? Output files can be read at leisure, values can be searched for, etc. ? The following describes how to use files for input and output. ? We will concentrate on text files only ? Keep in mind that a file can hold whatever kind of data you want: ints to represent grades, floats to represent temperatures, strings to represent names, or any combination of values ? In general terminology, each line of a file is often referred to as a record ? For example, a line from a file containing student info might look like

doe john 85 78 82

? To process an input file, you will need to know the structure of its records, so your program knows how many pieces of data each line holds and what data type each is supposed to represent

In the above example, we would need to know that we should expect 2 strings followed by 3 integers

1

Python: File I/O - Streams ? Stream is used to refer to any input or output device

? The terminology reflects the fact that data/bytes flow from one place (the source) to another (the destination)

? A stream can be associated with any I/O device: the keyboard, screen, secondary storage, etc. ? The standard streams: 1. sys.stdin (standard input - the keyboad) 2. sys.stdout (standard output - the screen) 3. sys.stderr (standard error - the screen)

? Our main concern is with file I/O

2

Python: File I/O - Stream Types ? Python recognizes three types of streams:

1. Text ? This type processes str objects ? Human-readable ? Each byte represents an ASCII character ? Usually broken into lines, each terminated by a newline ('\n') character

2. Binary (sometimes called buffered) ? Not human-readable ? Data maintained in binary format: does not interpret bytes ? Used for all data that is not textual (or text that you want to manually process)

3. Raw (sometimes called unbuffered) ? Not human-readable ? Lowest level: underlies binary and text ? Generally not used in a program

? Programs must know file type before using ? The following concentrates on text files

3

Python: File I/O - Opening a File ? A stream must be opened before it can be used ? Syntax (not complete):

? file name specifies the stream to be opened, which may include a path ? mode specifies how the stream can be accessed

mode description "r" open for reading (default) "w" open or create for writing, discarding existing content "a" open or create for appending at end "x" create, fail if exists "b" binary mode "t" text mode (default) "r+" open for reading and writing "w+" open for writing and reading

Modes can be combined; for example: rb, etc. ? buffering is an integer that sets buffering policy

A buffer is a part of memory set aside for intermediate storage of data passing from source to destination

mode description

0

turn buffering off (binary only)

1

line buffering (text only)

> 1 number of bytes to use for buffer

none use default policy (line for text)

? Returns a pointer to the stream object if successful, an OSError is raised on failure

4

Python: File I/O - Opening a File (2) ? Examples:

1. Open a text file for reading: infile = open("myFile.txt") infile = open("myFile.txt", "r") infile = open("myFile.txt", "rt")

2. Open a binary file for reading: infile = open("myFile.txt", "rb")

3. Open a text file for writing: infile = open("myFile.txt", 'w')

? Once a file has been opened, file methods are used to work with it

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download