2011-07-12 32 views
0

是否有将python'with'语句转换为可以在以前版本的python中使用的格式的方法。关于这个问题的4个月的工作。与之前的同行相比效率更高,但效率在这里并不重要。'With'in pre python 2.5

回答

0

正如S.Lott所说,试着最后应该处理with子句的工作。我不知道,其实with捕捉任何错误,所以考虑到假设:

with open(file_name,mode) as name: # Or whatever expression 
    do_this() 

可以

try: 
    name = open(filename,mode) # Or whatever expression 
    do_this() 
finally: 
    name.close() 
+0

更换应该是不'最后:如果name:name.close()' ? –

+0

不,名称的分配应该在try块之外进行。 –