简体   繁体   中英

pass variable in to command in python

i'm writing a python script to execute shell command, and i'm taking arguments and i want to pass the value of that to the command

#!/usr/bin/env python

import commands
import subprocess
import sys

command = commands.getoutput('fs_cli -x "sofia profile external restart"')

this code works fine

when i try to take the argument and pass to command it fails

command = commands.getoutput('fs_cli -x "sofia profile" + sys.argv[1] + " restart"')

supp folks

你应该写:

command = commands.getoutput('fs_cli -x "sofia profile ' + sys.argv[1] + ' restart"')

看看argparsesubprocess

One of the way to do this is to convert your command that you want to execute into string and then execute it as eval()

example : eval(expression/command in string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM