Saturday, 5 December 2015

Python: with : Predefined Clean-up Actions


You can get the file object using ‘with’ keyword. One advantage of ‘with’ keyword is, file is properly closed after its suite finishes, even if an exception is raised on the way.
>>> with open('/Users/harikrishna_gurram/abc.txt', 'r') as f:
...     read_data = f.read()
...     print(read_data)
... 
0123456789abcdef
>>> 
>>> f.closed
True



Previous                                                 Next                                                 Home

No comments:

Post a Comment