Implementation of stdin and stdout
In the problem (except for the solution algorithm itself), it is necessary to implement data reception via stdin
, and output via stdout
. Before that, it usually implemented reception via input
, and output via print
, or simply returned a value without printing via return
.
How to accept data using stdin
/stdout
I don't get it.
A huge request, give a link or a sample code to understand.
3 answers
By default input()
reads data from stdin
, print()
prints data in stdout
. So you can consider your task solved.
In Python stdin
, stdout
submitted by sys.stdin
, sys.stdout
objects (text streams, as a rule), which in general can be of any type (if their interface is sufficiently file-like) and can be overridden by anyone (IDLE, bpython, ipython, IDE, win-unicode-console, etc). Sometimes it is enough to provide an object that supports a single .write()
method, if you only need to support the print()
function. In other cases, even an instance io.TextIOWrapper
(type sys.stdin
/sys.stdout
default) may be insufficient if .fileno()
does not return a real file descriptor (see details in Redirect stdout to a file in Python?).
When starting Python, sys.stdin
/sys.stdout
usually point to standard I/O streams, inherited from the parent process or received from the console. Interactive I / O is usually associated with a terminal. From the shell, it is easy to redirect input/output from a file, pipe (pipe)
$ python ваша-программа.py <входной-файл
# `sys.__stdin__` это входной-файл
$ echo abc | python ваша-программа.py
# `sys.__stdin__` это pipe (`echo` пишет с одного конца, мы читаем с другого)
Working directly with sys.stdin
, sys.stdout
the same as with other text files. For example, to read text lines from standard input and write the entered characters (Unicode codepoint) in each line in reverse order to standard output:
#!/usr/bin/env python3
import sys
for line in sys.stdin:
print(line.rstrip('\n')[::-1])
Encoding used sys.stdin
/sys.stdout
, how to convert text to bytes and back again may depend on the environment. To avoid krakozyab or UnicodeEncodeError
exceptions due to working with arbitrary Unicode characters in Windows console and on other platforms follow the links provided here that show win-unicode-console
(PEP 528 may abolish this package), LC_*
(locale), PYTHONIOENCODING
solutions. PEP-538, PEP-540 implemented in Python 3.7, forces Python to use utf-8 in more cases, making problems with I/O encodings much less likely by default.
print()
this is a handy wrapper around sys.stdout.write()
. input()
it can often be considered as a wrapper around sys.stdin.readline()
, designed for interactive input (support for input history, editing with readline
of the module, if available). For advanced support for interactive input in the terminal, look at prompt_toolkit
:
#!/usr/bin/env python
from prompt_toolkit import prompt # $ pip install prompt_toolkit
if __name__ == '__main__':
answer = prompt('Give me some input: ')
print('You said: %s' % answer)
stdin
and stdout
are file-like objects provided by the OS.
To read and write to them, you need to import sys - import sys
.
sys.stdin.read()
use to read from stdin
to write to stdout
, you can use print
(this is how it is used - the most common method of writing to stdout). i.e. print
writes to sys.stdout
.
Example:
import sys
str = sys.stdin.read()
print str
def countdistinctfivisors(n, start=2):
if n == 1:
return 1
else:
result = 0
for i in range(start, n+1):
if n % i == 0:
result += countdistinctfivisors(n // i, i + 1)
return result
print(countdistinctfivisors(int(input())))