0

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?

4
  • 1
    You seem to be missing a comma after Code.exe" (Popen takes a list of strings, rather than one string), and a missing double quote on Python'. Also the loop does not repeat. The code shown will always throw DivisionByZero and then the loop will break out and stop the function Commented Jun 28, 2022 at 21:25
  • Also, this should open a brand new window to that folder rather than focus an existing one. For focusing any GUI window, you need to use Windows APIs, maybe this library - pypi.org/project/PyGetWindow Commented Jun 28, 2022 at 21:31
  • Hi @OneCricketeer Yes, the looping error was on purpose to demonstrate what should happen when there is an error. Regarding the other details, perfect, I understood completely, I need to separate and not unite, it worked perfectly. Commented Jun 28, 2022 at 21:32
  • 1
    @OneCricketeer I'll read the documentation that you put the url, for me it worked perfectly because my VS Code doesn't accept two instances of the same project, so it focuses on the one that is already open. Thanks for all the information! Commented Jun 28, 2022 at 21:34

1 Answer 1

1

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' ]) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.