Home > OS >  If an error occurs in code execution, focus on the Visual Studio Code window that the code running
If an error occurs in code execution, focus on the Visual Studio Code window that the code running

Time:06-29

I open my VS Code, open my project folder and put my code to run a Python file in loop and I'm going to do other activities on the computer, the VS Code window is in the background.

But the code encounters an error, at this point I would like the Visual Studio Code window running this code overlay all other windows and appear on the screen with focus.

If I want to create a shortcut that I click and it opens VS Code directly in the project folder, I need to put the path of the VS Code executable and after them put the path of the project folder:

"C:\Users\Computador\AppData\Local\Programs\Microsoft VS Code\Code.exe" "C:\Users\Computador\Desktop\Python

So I tried to use it:

import subprocess
from time import sleep

def main() -> None:
    while True:
        try:
            a = 1
            b = 0
            c = a/0
        except:
            subprocess.Popen([r'"C:\Users\Computador\AppData\Local\Programs\Microsoft VS Code\Code.exe" "C:\Users\Computador\Desktop\Python'])
            break
        sleep(60)

if __name__ == '__main__':
    main()

But get this error:

PermissionError: [WinError 5] Access Denied

How should I proceed to avoid this error?

CodePudding user response:

The error could be more clear, but it is trying to run the whole string as an executable, which doesn't exist (therefore there is no access to it)

To provide arguments to an executable, it's best to create a list.

subprocess.Popen([
    r'C:\Users\Computador\AppData\Local\Programs\Microsoft VS Code\Code.exe',
    r'C:\Users\Computador\Desktop\Python'
])
  • Related