$1.57

"with" statement in Python

Ask a question+
1

What is the Python with statement designed to be used for?

with open('file_path', 'w') as file: 
    file.write('Something')
add comment

2 Answers

0

You can try this:
with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner.
For Eg:
with open(‘file_path’, ‘w’) as file:
file.write(‘hello world !’)

add comment
0

You can try this:
with statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner.
For Eg:
with open(‘file_path’, ‘w’) as file:
file.write(‘hello world !’)

add comment

Your Answer