0

I am trying to write a short program that would pass variables for a os.system(). Where the variables are defined as following:

code = 'code.py'
input_file = 'input.xxxx'
output_file = 'output.xxxx'


import os
os.system("python code input_file output_file")

The code works when i specify it as

import os
os.system("python code.py input.xxxx output.xxxx")

But not with variables. Your help will be greatly appreciated.

2 Answers2

1

Your first attempt at it is passing a string made up of variable names, not their values. Try this:

os.system("python {} {} {}".format(code, input_file, output_file)) # Python 3.x

or

os.system("python {0} {1} {2}".format(code, input_file, output_file)) # Python 2.x

That will insert the variable values into the string for you.

mklement0
  • 382,024
  • 64
  • 607
  • 775
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
  • Good! I'm glad it helped. You should [mark one of the available answers as accepted](http://stackoverflow.com/help/accepted-answer) when you have one that meets your needs. – skrrgwasme Jul 15 '14 at 15:56
0

python doesnt recognize the variable names inside quotations. youll have to concatenate the string and the variables like so:

os.system("python " + code + " " + input_file + " " + output_file);

now i forget if python + automatically adds a space or not so you might not need those " " or the space after python

awarrier99
  • 3,628
  • 1
  • 12
  • 19