For opening files, I\'m used to the apparently older syntax:
f = open(\"sub_ranks.txt\",\"r+\")
for line in f:
...
f.close()
I\'ve been tol
From the python docs, I see that with is a syntactic sugar for the try/finally blocks. So,
Is a file object "close" statement still needed in the second example, when the "with" statement is being used?
No.
From the Python docs:
The ‘with‘ statement clarifies code that previously would use try...finally blocks to ensure that clean-up code is executed. In this section, I’ll discuss the statement as it will commonly be used. In the next section, I’ll examine the implementation details and show how to write objects for use with this statement.
The ‘with‘ statement is a control-flow structure whose basic structure is:
with expression [as variable]: with-block
The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has enter() and exit() methods).
Here's another article that makes it clear.