I'm trying to write a script which will take in a parameter beta, and a number of iterations k, and then print "beta" k times.
I want to be able to specify beta and k at the command line, and then run this script from there. I've been using optparse as follows:
import io
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-b", "--beta",type="float", dest="input_parameter")
parser.add_option("-k", "-iterations",type="int",dest="number_of_iterations")
(options, args) = parser.parse_args()
Beta = options.input_parameter
K = options.number_of_iterations
with io.open("output_when_beta_equals_{0}.txt".format(Beta), "a") as f:
for i in range(K):
f.write(u"beta = {0}, this is iteration number {1}.".format(Beta, i))
I then try to run
python toy_script.py -b $"0.3" -k $20
from the command line, and then the resulting "output_when_beta_equals_0.3.txt" file ends up empty.
I'm trying to work out what I need to do to fix this so that I instead get 20 lines of
beta = 0.3, this is iteration number 0. beta = 0.3, this is iteration number 1.
.... etc., in the output file.
Your code works, after replacing -iterations
by --iterations
and calling it like this:
python toy_script.py -b0.3 -k20
(tested on Windows)