[Tutor] Why do you have to close files?
0101amt at gmail.com
Fri Jan 27 00:20:32 CET 2012
Exercise 17, extra credit 6 Learn python the hard way: Find out why
you had to do output.close() in the code.
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
input = open(from_file)
indata = input.read()
print "The input file is %d bytes long" % len(indata)
print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
output = open(to_file, 'w')
print "Alright, all done."
I don't get it. If you don't close input and output it works exactly
the same as if you would close them, so why do you have to do
output.close() and input.close()?
Also does it matter if you do: input.close() and then output.close()?
Is there an order to follow?
Thanks in advance, amt.
More information about the Tutor