I have code like this as part of a more complex problem: with open(name, 'r') as FD: buf = os.fstat(FD.fileno()) mode = S_IMODE(buf.st_mode) and I get the following error: mode = S_IMODE(buf.st_mode) ^^^ UnboundLocalError: cannot access local variable 'buf' where it is not associated with a value If I change the code to this everything works: with open(name, 'r') as FD: # do some other stuff with FD buf = os.fstat(FD.fileno()) mode = S_IMODE(buf.st_mode) Why? I want to do as little as possible inside the with block so the resources are released quickly. What is the correct syntax to get information out of the with block? Continue reading...