I have a Python script that calls an executable program with various arguments (in this example, it is ‘sqlpubwiz.exe’ which is the ‘Microsoft SQL Server Database Publishing Wizard’):
import os sqlpubwiz = r''C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz.exe'' server = 'myLocalServer' database = 'myLocalDatabase' connection_values = ['server=' + server, 'database=' + database, 'trusted_connection=true'] connection_string = ';'.join(connection_values) dbms_version = '2000' sqlscript_filename = 'CreateSchema.sql' args = [ sqlpubwiz, 'script', '-C ' + connection_string, sqlscript_filename, '-schemaonly', '-targetserver ' + dbms_version, '-f', ] cmd = ' '.join(args) os.system(cmd)
This code runs properly but I have would like to get into the habit of using subprocess since it is intended to replace os.system. However, after a few failed attempts, I can not seem to get it work properly.
How would the above code look like if it was converted to use subprocess in place of os.system?
It would look pretty much the same. But the path should not be r”whatever the path is”. Because that gives me an error. You want ‘the path with escaped backslashes’ or r’the path without escaping’.
Also args should be of the form [‘-arg’, ‘args’] instead of [‘arg argsval’].