mahmood
mahmood

Reputation: 24715

pass multiple argument to sys.stdout.write

Is it possible to pass multiple argument to sys.stdout.write? All examples I saw uses one parameter.

The following statements are incorrect.

sys.stdout.write("\r%d of %d" % read num_lines)
Syntax Error: sys.stdout.write

sys.stdout.write("\r%d of %d" % read, num_lines)
not enough arguments for format string

sys.stdout.write("\r%d of %d" % read, %num_lines)
Syntax Error: sys.stdout.write

sys.stdout.write("\r%d of %d" % read, num_lines)
not enough arguments for format string

What should I do?

Upvotes: 6

Views: 12026

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You need to put your variables in a tuple :

>>> read=1
>>> num_lines=5
>>> sys.stdout.write("\r%d of %d" % (read,num_lines))
1 of 5>>> 

Or use str.format() method:

>>> sys.stdout.write("\r{} of {}".format(read,num_lines))
1 of 5

If your arguments are within an iterable you can use unpacking operation to pass them to string's format() attribute.

In [18]: vars = [1, 2, 3]
In [19]: sys.stdout.write("{}-{}-{}".format(*vars))
1-2-3

Upvotes: 9

Related Questions