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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With