In Python (>2.7) does the code :
open('tick.001', 'w').write('test')
has the same result as :
ftest = open('tick.001', 'w')
ftest.write('test')
ftest.close()
and where to find documentation about the ‘close’ for this inline functionnality ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
close()here happens when thefileobject is deallocated from memory, as part of its deletion logic. Because modern Pythons on other virtual machines — like Java and .NET — cannot control when an object is deallocated from memory, it is no longer considered good Python toopen()like this without aclose(). The recommendation today is to use awithstatement, which explicitly requests aclose()when the block is exited:If you do not need a name
ffor the file, then you can omit theasclause from the statement: