How to open OBS Studio using Python

1.1k views Asked by At

I am trying to figure out how to open OBS Studio using a python script. I have used either of the two little code snippets below, but none work when ran separately. However, when ran together, my OBS Studio opens, however I am met with the following error:

import os
os.startfile("C:\\OBS_Studio")

import subprocess
subprocess.Popen("C:\\OBS_Studio")

enter image description here

So my question is, am I doing something incorrectly because I don't know if I add more to the script, if that error message will kill the process, unless I add some sort of error-handling code. However, I don't think that will be necessary as this message is almost assuredly due to user error on my part. I have added an image of my OBS Studio's location as well for reference. enter image description here

Additionally, just incase the issue is due to me referencing the shortcut, here is a screenshot of the .exe's file path. And when I convert the two snippets of code to reference the .exe path, the same error pops up, however OBS Studio doesn't open at all.

import os
os.startfile("C:\Program Files\obs-studio\bin\64bit\obs64.exe")
import subprocess
subprocess.Popen("C:\Program Files\obs-studio\bin\64bit\obs64.exe")

enter image description here

2

There are 2 answers

0
bavistr On

In the example with the absolute path to the exe, make sure you escape the backslashes by using \. Your code should look like this:

import os
os.startfile("C:\\Program Files\\obs-studio\\bin\\64bit\\obs64.exe")
import subprocess
subprocess.Popen("C:\\Program Files\\obs-studio\\bin\\64bit\\obs64.exe")
0
MingJie-MSFT On

In the string of the open file, the address string information similar to c:\user\desktop will inevitably be involved, which conflicts with the escape function in Python string. For example, \n means newline.

We can use r"c:\user\desktop" or c:\\user\\desktop to avoid Python escaping strings.