Subprocess not opening files
Question:
I am writing a program to open other programs for me. os.system() would always freeze my app, so I switched to subprocess.
I did some research and this is how a tutorial told me to open a program.
I have only replaced the path for my variable, which contains the path.
After I run this, only a commabd prompt window opens and nothing else. How can I fix this?
Code:
from subprocess import Popen
filename1 = "C:/Program Files/Google/Chrome/Application/chrome.exe"
Popen(["cmd", "/c", "start", filename1)
Answers:
You need to create a single string with double quotes around it. In Python terms, you basically want r'"c:torturethanks Microsoft"'
where the single quotes and the r
create a Python string, which contains the file name inside double quotes.
from subprocess import Popen
filename1 = "C:/Program Files/Google/Chrome/Application/chrome.exe"
Popen(["cmd", "/c", "start", f'"{filename1}"'])
Quoting with CMD is always bewildering; maybe think about ways you can avoid it (or Windows altogether, if you have a choice).
import subprocess
filename1 = "C:Program FilesGoogleChromeApplicationchrome.exe"
subprocess.Popen(filename1)
I am writing a program to open other programs for me. os.system() would always freeze my app, so I switched to subprocess.
I did some research and this is how a tutorial told me to open a program.
I have only replaced the path for my variable, which contains the path.
After I run this, only a commabd prompt window opens and nothing else. How can I fix this?
Code:
from subprocess import Popen
filename1 = "C:/Program Files/Google/Chrome/Application/chrome.exe"
Popen(["cmd", "/c", "start", filename1)
You need to create a single string with double quotes around it. In Python terms, you basically want r'"c:torturethanks Microsoft"'
where the single quotes and the r
create a Python string, which contains the file name inside double quotes.
from subprocess import Popen
filename1 = "C:/Program Files/Google/Chrome/Application/chrome.exe"
Popen(["cmd", "/c", "start", f'"{filename1}"'])
Quoting with CMD is always bewildering; maybe think about ways you can avoid it (or Windows altogether, if you have a choice).
import subprocess
filename1 = "C:Program FilesGoogleChromeApplicationchrome.exe"
subprocess.Popen(filename1)