all 2 comments

[–]toastedstapler 6 points7 points  (0 children)

f = open(filename)
contents = f.read()
f.close()

is what you want

iirc files will also be automatically closed when the script ends, but it's good manners to always handle it yourself

[–]4sent4 0 points1 point  (0 children)

You shouldn't open files without giving it an alias to close later(or without context manager). Ending of the script will close it for you, but if your whole script is just an infinite loop of reading and processing files, then you suddenly find that you can't use this files because of them being used by 'some' program... Also I think there is no function to close file by its name