Python Forum

Full Version: Equivalent Python code from VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys

Refer to the below code which is developed on Excel VBA, the code essentially opens Space Gass and passes the text to the Space Gass.
What would the equivalent be on Python?

I currently have managed to open Space Gass from Python using "subprocess.call()"

    Dim strSGProgram As String
    strSGProgram = """" & "C:\Program Files (x86)\SPACE GASS 14\sgwin.exe" & """"
    Dim strSGScript As String
    strSGScript = " -s " & """" & "c:\temp\SGScriptDemo\Save and close script.txt" & """"
    Call Shell(strSGProgram & strSGScript, vbMinimizedNoFocus)
I guess you could do something like this:

import subprocess

strSGProgram = r'"C:\Program Files (x86)\SPACE GASS 14\sgwin.exe"'
strSGScript = r' -s "c:\temp\SGScriptDemo\Save and close script.txt"'

command = strSGProgram + strSGScript
subprocess.run(command, creationflags=subprocess.CREATE_NO_WINDOW, shell=True)
Edit: updated to subprocess.run() since subprocess.call() is deprecated.
happy wheels
Thank you, that resolved it
To achieve the equivalent functionality in Python, you can use the subprocess module to call the Space Gass executable and pass the script file as an argument.
import subprocess

from subprocess import CREATE_NO_WINDOW

# use better names
# use forward slahes to avoid complications with backslashes
program = "C:/Program Files (x86)/SPACE GASS 14/sgwin.exe"
script = "C:/temp/SGScriptDemo/Save and close script.txt"

subprocess.run([program, "-s", script], creationflags=CREATE_NO_WINDOW)
Don't use shell=True. By default shell=False.