input and output copyright © software carpentry 2010 this work is licensed under the creative...

87
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution Lice See http://software-carpentry.org/license.html for more informati Python

Upload: adriana-arbon

Post on 31-Mar-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Input and Output

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Python

Page 2: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Been using print to see what programs are doing

Page 3: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Been using print to see what programs are doing

How to save data to files?

Page 4: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Been using print to see what programs are doing

How to save data to files?

And read data from them?

Page 5: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Been using print to see what programs are doing

How to save data to files?

And read data from them?

Python's solution looks very much like C's

Page 6: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Been using print to see what programs are doing

How to save data to files?

And read data from them?

Python's solution looks very much like C's

– A file is a sequence of bytes

Page 7: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Been using print to see what programs are doing

How to save data to files?

And read data from them?

Python's solution looks very much like C's

– A file is a sequence of bytes

– But it's often more useful to treat it as a sequence

of lines

Page 8: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Sample data file

Three things are certain:Death, taxes, and lost data.Guess which has occurred.

Errors have occurred.We won't tell you where or why.Lazy programmers.

With searching comes lossand the presence of absence:"My Thesis" not found.

A crash reducesyour expensive computerto a simple stone.

Page 9: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

Page 10: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

byte

s

Page 11: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

byte

s

Assume 1-to-1 for now

Page 12: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

byte

s

Assume 1-to-1 for now

Revisit later

Page 13: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Page 14: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Create a file object

Page 15: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

File to connect to

Page 16: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

To read

Page 17: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Now holds file object

Page 18: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Read entire content

of file into a string

Page 19: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Now has a copy of

all the bytes that were

in the file

Page 20: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Disconnect from the file

Page 21: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Disconnect from the file

Not strictly necessary

in small programs, but

good practice

Page 22: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Report how many

characters were read

Page 23: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)

Report how many

characters were read

byte

s

Page 24: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

How many characters in a file?

reader = open('haiku.txt', 'r')data = reader.read()reader.close()print len(data)293

Page 25: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

Page 26: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()

Page 27: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()

Read (at most) 64 bytes

Page 28: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()

Read (at most) 64 bytes

Or the empty string

if there is no more data

Page 29: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()

Keep looping as long as

the last read returned

some data

Page 30: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()

Do something with

the data

Page 31: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()

(Try to) reload

Page 32: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close() Should be 0 (or the loop

would still be running)

Page 33: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()64646464370

Page 34: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()64646464370

Don't do this unless

Page 35: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

If the file might be large, better to read in chunks

Don't do this unless the file really

might be very large (or infinite)

reader = open('haiku.txt', 'r')data = reader.read(64)while data != '': print len(data) data = reader.read(64)print len(data)reader.close()64646464370

Page 36: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

More common to read one line at a time

Page 37: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

More common to read one line at a time

reader = open('haiku.txt', 'r')line = reader.readline()total = 0count = 0while line != '': count += 1 total += len(line) line = reader.readline()reader.close()print 'average', float(total) / float(count)

Page 38: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

More common to read one line at a time

reader = open('haiku.txt', 'r')line = reader.readline()total = 0count = 0while line != '': count += 1 total += len(line) line = reader.readline()reader.close()print 'average', float(total) / float(count)

Read a single line

Page 39: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

More common to read one line at a time

reader = open('haiku.txt', 'r')line = reader.readline()total = 0count = 0while line != '': count += 1 total += len(line) line = reader.readline()reader.close()print 'average', float(total) / float(count)

Keep looping until

no more lines in file

Page 40: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

More common to read one line at a time

reader = open('haiku.txt', 'r')line = reader.readline()total = 0count = 0while line != '': count += 1 total += len(line) line = reader.readline()reader.close()print 'average', float(total) / float(count)

(Try to) reload

Page 41: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

More common to read one line at a time

reader = open('haiku.txt', 'r')line = reader.readline()total = 0count = 0while line != '': count += 1 total += len(line) line = reader.readline()reader.close()print 'average', float(total) / float(count)19.53333333

Page 42: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often more convenient to read all lines at once

Page 43: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often more convenient to read all lines at once

reader = open('haiku.txt', 'r')contents = reader.readlines()reader.close()total = 0count = 0for line in contents: count += 1 total += len(line)print 'average', float(total) / float(count)

Page 44: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often more convenient to read all lines at once

reader = open('haiku.txt', 'r')contents = reader.readlines()reader.close()total = 0count = 0for line in contents: count += 1 total += len(line)print 'average', float(total) / float(count)

All lines in file

as list of strings

Page 45: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often more convenient to read all lines at once

reader = open('haiku.txt', 'r')contents = reader.readlines()reader.close()total = 0count = 0for line in contents: count += 1 total += len(line)print 'average', float(total) / float(count)

Loop over lines

with for

Page 46: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often more convenient to read all lines at once

reader = open('haiku.txt', 'r')contents = reader.readlines()reader.close()total = 0count = 0for line in contents: count += 1 total += len(line)print 'average', float(total) / float(count)19.53333333

Page 47: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

"Read lines as list" + "loop over list" is common idiom

Page 48: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

"Read lines as list" + "loop over list" is common idiom

So Python provides "loop over lines in file"

Page 49: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

"Read lines as list" + "loop over list" is common idiom

So Python provides "loop over lines in file"

reader = open('haiku.txt', 'r')total = 0count = 0for line in reader: count += 1 total += len(line)reader.close()print 'average', float(total) / float(count)

Page 50: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

"Read lines as list" + "loop over list" is common idiom

So Python provides "loop over lines in file"

reader = open('haiku.txt', 'r')total = 0count = 0for line in reader: count += 1 total += len(line)reader.close()print 'average', float(total) / float(count)

Assign lines of text in file

to loop variable one by one

Page 51: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

"Read lines as list" + "loop over list" is common idiom

So Python provides "loop over lines in file"

reader = open('haiku.txt', 'r')total = 0count = 0for line in reader: count += 1 total += len(line)reader.close()print 'average', float(total) / float(count)19.53333333

Page 52: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Page 53: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

Page 54: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

Page 55: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

Same function

Page 56: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

File to write to

Page 57: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

File to write to

Created if it doesn't exist

Page 58: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

For writing instead

of reading

Page 59: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

Write a single string

Page 60: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

Write each string

in a list

Page 61: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

elementsHeNeArKr

Page 62: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements')writer.writelines(['He', 'Ne', 'Ar', 'Kr'])writer.close()

elementsHeNeArKr

Python only writes what you tell it to

Page 63: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements\n')writer.writelines(['He\n', 'Ne\n', 'Ar\n', 'Kr\n'])writer.close()

Have to provide end-of-line characters yourself

Page 64: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Put data in a file using write or writelines

writer = open('temp.txt', 'w')writer.write('elements\n')writer.writelines(['He\n', 'Ne\n', 'Ar\n', 'Kr\n'])writer.close()

elementsHeNeArKr

Page 65: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often simpler to use print >>

Page 66: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often simpler to use print >>

writer = open('temp.txt', 'w')print >> writer, 'elements'for gas in ['He', 'Ne', 'Ar', 'Kr']: print >> writer, gaswriter.close()

Page 67: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often simpler to use print >>

writer = open('temp.txt', 'w')print >> writer, 'elements'for gas in ['He', 'Ne', 'Ar', 'Kr']: print >> writer, gaswriter.close()

Specify open file after >>

Page 68: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Often simpler to use print >>

writer = open('temp.txt', 'w')print >> writer, 'elements'for gas in ['He', 'Ne', 'Ar', 'Kr']: print >> writer, gaswriter.close()

print automatically adds the newline

Page 69: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file

Page 70: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file

reader = open('haiku.txt', 'r')data = reader.read()reader.close()writer = open('temp.txt', 'w')write.write(data)writer.close()

Page 71: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file

reader = open('haiku.txt', 'r')data = reader.read()reader.close()writer = open('temp.txt', 'w')write.write(data)writer.close()

Read all

Page 72: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file

reader = open('haiku.txt', 'r')data = reader.read()reader.close()writer = open('temp.txt', 'w')write.write(data)writer.close()

Write all

Page 73: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file

reader = open('haiku.txt', 'r')data = reader.read()reader.close()writer = open('temp.txt', 'w')write.write(data)writer.close()

Probably won't work with a terabyte...

Page 74: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file

reader = open('haiku.txt', 'r')data = reader.read()reader.close()writer = open('temp.txt', 'w')write.write(data)writer.close()

Probably won't work with a terabyte...

…but we probably don't care

Page 75: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 2)

Page 76: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 2)

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: writer.write(line)reader.close()writer.close()

Page 77: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 2)

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: writer.write(line)reader.close()writer.close()

Assumes the file is text

Page 78: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 2)

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: writer.write(line)reader.close()writer.close()

Assumes the file is text

Or at least that the end-of-line character appears

frequently

Page 79: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

This version doesn't make an exact copy

Page 80: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

This version doesn't make an exact copy

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: print >> writer, linereader.close()writer.close()

Page 81: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

This version doesn't make an exact copy

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: print >> writer, linereader.close()writer.close()

Python keeps the newline when reading

Page 82: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

This version doesn't make an exact copy

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: print >> writer, linereader.close()writer.close()

Python keeps the newline when reading

print automatically adds a newline

Page 83: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

This version doesn't make an exact copy

reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')for line in reader: print >> writer, linereader.close()writer.close()

Python keeps the newline when reading

print automatically adds a newline

Result is double-spaced output

Page 84: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 3)

Page 85: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 3)

BLOCKSIZE = 1024reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')data = reader.read(BLOCKSIZE)while len(data) > 0: writer.write(data) data = reader.read(BLOCKSIZE)reader.close()writer.close()

Page 86: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Input and Output

Copy a file (version 3)

BLOCKSIZE = 1024reader = open('haiku.txt', 'r')writer = open('temp.txt', 'w')data = reader.read(BLOCKSIZE)while len(data) > 0: writer.write(data) data = reader.read(BLOCKSIZE)reader.close()writer.close()

(Needlessly?) harder to understand

Page 87: Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

October 2010

created by

Greg Wilson

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.