In Python, how do I use a subprocess instead of os.system?

I have a Python script that calls an executable program with various arguments (in this example, "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 works correctly, but I would like to get used to using subprocess , as it is intended to replace os. system. However, after several unsuccessful attempts, I cannot get it to work correctly.

What would the above code look like if it was converted to use a subprocess instead of os.system?

+3
source share
7 answers
import subprocess
p=subprocess.Popen(args, stdout=subprocess.PIPE)
print p.communicate()[0]

. r ' " ". . " " " ".

args ['-arg', 'args'] ['arg argsval'].

+5

.

sqlpubwiz = r'"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz.exe"'

:

sqlpubwiz = r'C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz.exe'

, , .

subprocess.call(args) ( join , )

(os.system ), subprocess :

result = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
print result
+4

Carlos Rendon ( nosklo):

# import os
import subprocess    

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)

subprocess.call(args)

(: , , .)

+4

FYI, subprocess list2cmdline(), , Popen.

:

'"C:\\Program Files\\Microsoft SQL Server\\90\\Tools\\Publishing\\sqlpubwiz.exe" script "-C server=myLocalServer;database=myLocalDatabase;trusted_connection=true" CreateSchema.sql -schemaonly "-targetserver 2000" -f'

"-C server=myLocalServer;database=myLocalDatabase;trusted_connection=true" "-targetserver 2000".

:

args = [
        sqlpubwiz,
        'script',
        '-C', connection_string,
        sqlscript_filename,
        '-schemaonly',
        '-targetserver', dbms_version,
        '-f',
]

:

'"C:\\Program Files\\Microsoft SQL Server\\90\\Tools\\Publishing\\sqlpubwiz.exe" script -C server=myLocalServer;database=myLocalDatabase;trusted_connection=true CreateSchema.sql -schemaonly -targetserver 2000 -f'

, , , args, .

+4

, , .

, - , .., pexpect. , , , , . .

0

Windows '/' , , . , , , .

0

Please remember that the os.system uses a shell, so you should really go through

shell=True

constructor / call Popen to properly emulate it. Of course, in fact, you do not need a shell, but it is.

-one
source

All Articles