I have the following code:
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
And in one of the exercises in my book it says "Have your script also do a close() on the txt and txt_again variables. It’s important to close files when you are done with them.
How can I close the variables?
txt.close();txt_again.close(). Btw you're not closing variables. You're closing the file objects.withcan be used to alleviate manually management. e.g see docs.python.org/3/reference/… , stackoverflow.com/questions/4042995/…with, which will handle the closing for you even in case of exception.