open(...) returns a link to a file object that calls read , which reads a file, returns a string object, calls write to it, returns None , none of which have a close attribute.
>>> help(open) Help on built-in function open in module __builtin__: open(...) open(name[, mode[, buffering]]) -> file object Open a file using the file() type, returns a file object. This is the preferred way to open a file. >>> a = open('a', 'w') >>> help(a.read) read(...) read([size]) -> read at most size bytes, returned as a string. If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given. >>> help(a.write) Help on built-in function write: write(...) write(str) -> None. Write string str to file. Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written.
Here are some ways to fix this:
>>> file = open(from_file) >>> content = file.read() >>> file.close()
or with python> = 2.5
>>> with open(from_file) as f: ... content = f.read()
with will make sure the file is closed.
source share