Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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?

>Solution :

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'
])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading