[Python] What is a with statement?

[Python] What is a with statement? Explain the usage and meaning with actual examples

With is used in the explanation of ExcelWriter, I investigated the meaning and usage, so I summarized it.

Official page (overview)Official page (example)


## Use and content of the with statement ** ■ Applications ** ** Used for processing that is a set of "start" and "end" **.

-Open the file to read and write. -Access the DB and exit after processing.


** ■ Processing content ** In the process that "start" and "end" are set If you "start" using the with statement, it will ** automatically "end" ** after the process is executed.
** ■ Merit ** -You do not have to write the termination process. -Do not forget to write the end process.
## Basic syntax of with ``` with ① Start processing as ② Variable: ③ Processing ```

① Start processing └ Describe the process to start communication such as opening a file.

② Variable └ Variable (abbreviation) for calling process ①

③ Processing └ Process to be executed. When this process is completed, the file is automatically closed (communication is disconnected).


## Code example using the with statement In the official python documentation, It is used in the open function that opens a file and ExcelWriter that opens Excel.

open


with open('spamspam.txt', 'w', opener=opener) as f:
    print('This will be written to somedir/spamspam.txt', file=f)

Built-in function open

ExcelWriter


with ExcelWriter('path_to_file.xlsx') as writer:
    df.to_excel(writer)

Official page of ExcelWriter


Recommended Posts

[Python] What is a with statement?
What is a python map?
Python for statement ~ What is iterable ~
What is python
What is God? Make a simple chatbot with python
What is Python
What is a dog? Python installation volume
What I did with a Python array
What is a distribution?
[Python] What is Pipeline ...
What is a terminal?
What is a hacker?
What is a pointer?
[Python] What is virtualenv
What are you comparing with Python is and ==?
Tell me what a conformal map is, Python!
order_by ('-created_at') ← What is "ー"? ?? ?? [Beginner learns python with a reference book]
[Introduction to Python] What is the method of repeating with the continue statement?
What is a decision tree?
Addition with Python if statement
Python list is not a list
What is a Context Switch?
Make a fortune with Python
[Python] Python and security-① What is Python?
What is a super user?
[Python] * args ** What is kwrgs?
What is a system call
[Definition] What is a framework?
Create a directory with python
What is a callback function?
Python Basic Course (1 What is Python)
Basics of Python learning ~ What is a string literal? ~
What is Python? What is it used for?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Determine if a string is a time with a python regular expression
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
[python] Reverse with slices! !! (There is also a commentary on slices!)
[Python] What is @? (About the decorator)
A memo when face is detected with Python + OpenCV quickly
Create a virtual environment with Python!
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
I made a fortune with Python.
[python] What is the sorted key?
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
What to do with PYTHON release?
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
What is the python underscore (_) for?
Let's make a graph with python! !!
Python> What is an extended slice?
[Python] Inherit a class with class variables
I made a daemon with Python
Turn an array of strings with a for statement (Python3)