/    /  Python Pre-defined Clean-up actions

Pre-defined Clean Up Actions

In Python, pre-defined clean up actions are mostly used to close the files. When we are executing some operations like read and write in the file, generally files are left open for long period of time. This may not cause any serious problem in simple scripts, but its sure causes trouble while working with larger applications. This pre-defined clean up actions automatically closes the file after its suite finishes, even though an exception raised on the way. It can used by the keyword “with”.

Python Code:

with open('C:\\Users\\sriharshithaghali\\Documents\\mynotes\\examples.txt', 'r') as f:
    read_data = f.read()
    print(read_data)
f.closed

Output:

True