It's useful when you have two or more linked operations that you'd like to run as a pair or multiple times with a code block in between. Opening a file, modifying it, and then closing it is a classic example:
with open('output.txt', 'w') as f:
f.write('Hi there!')
After the nested section of code, the above with statement will automatically shut the file. (Keep reading to find out how the close happens.) The benefit of using a with statement is that it guarantees that the file will be closed regardless of how the nested block exits. If an exception occurs before the end of the block, the file will be closed before an outer exception handler can catch it. If the nested block contained a return, continue, or break statement, the with statement would also shut the file automatically in those circumstances.