Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 863
mahmood Avatar asked Aug 03 '15 08:08

mahmood


People also ask

What is SYS stdout write ()?

A built-in file object that is analogous to the interpreter's standard output stream in Python. stdout is used to display output directly to the screen console. Output can be of any form, it can be output from a print statement, an expression statement, and even a prompt direct for input.

Does print write stdout?

print() and Standard Out. Every running program has a text output area called "standard out", or sometimes just "stdout". The Python print() function takes in python data such as ints and strings, and prints those values to standard out.


Video Answer


1 Answers

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
like image 147
Mazdak Avatar answered Sep 30 '22 17:09

Mazdak