I looked at a few results for how to do the task in the title. One of the most popular is an SOF answer here:
https://stackoverflow.com/a/49642681/1056563
In p