1

I used subprocess.popen as follows. The following linter suggestion occurs in vscode

try:
            p = subprocess.Popen(
                ["python", "app.py"]
            )
except:
            traceback.print_exc()
Consider using 'with' for resource-allocating operationspylint(consider-using-with)

But I don't know where to use 'with'

Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
kwsong0314
  • 149
  • 10

1 Answers1

2

Use with to assign the variable from subprocess.popen().

try:
    with subprocess.Popen(['python', 'app.py']) as p:
        # code here
except:
    traceback.print_exc()

Note that the try/except will also catch exceptions in the body of with. See this answer for how you can structure the code to just catch an exception from subprocess.Popen().

Barmar
  • 669,327
  • 51
  • 454
  • 560