1

I need to execute a system command in which there is a variable.In the code below

    os.system('SMILExtract -C config/demo/demo1_energy.conf -I user.wav -O csv/user.csv')

in the string "user.wav", user is a variable string and .wav is the constant string.How can include it within the os.system() command

1
  • FYI, it is usually better to use subprocess for things like this. It was designed specifically to replace several other, older modules and functions, such as os.system Commented Mar 5, 2014 at 16:35

4 Answers 4

3

Use a format string?

os.system('SMILExtract -C config/demo/demo1_energy.conf -I {0}.wav -O csv/user.csv'.format(somevar))

This can be made clean and readable by mapping out the things that can change into various named parameters:

cmd_template = 'SMILExtract -C {config_path} -I {wav_base}.wav -O {csv_path}'
os.system(cmd_template.format(
  config_path='config/demo/demo1_energy.conf',
  wav_base='user',
  csv_path='csv/user.csv',
))
Sign up to request clarification or add additional context in comments.

1 Comment

how to include if both csv path and output path is a variable ?
0

If the variable is named user, just use string concatenation:

os.system('SMILExtract -C config/demo/demo1_energy.conf -I ' + user + '.wav -O csv/user.csv')

Comments

0

One way to do this would be to use the call function of the subprocess module:

from subprocess import call
varname="test"
user=varname+str(".wav")
options="-C config/demo/demo1_energy.conf -I %s -O csv/user.csv"%(user)

call(["SMILExtract", options])

Comments

0

os.system('SMILExtract -C config/demo/demo1_energy.conf -I '+user+'.wav -O csv/user.csv')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.