# import sys

## `sys.stdin` and `sys.stdout`

`sys.stdin` and `sys.stdout` are useful when creating an interactive session. `sys.stdin` is a file object and we can loop it through its iterator:

```python
import sys

for line in sys.stdin:
    if line.strip() == 'exit':
        break
    sys.stdout.write(f">> {line}")
```

## Status Bar

Build a status bar using `sys.stdout.write`. The `\r` moves the cursor to the beginning of the line:

```python
import time
import sys

for i in range(101):
    time.sleep(0.1)
    sys.stdout.write(f"{i} [{'#' * i}{'.' * (100 - i)}]\r")
sys.stdout.write('\n')
```

## `input()` vs. `sys.stdin.readline()`

| input()                                                                     | sys.stdin.readline()                                                                                           |
| --------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| The input takes input from the user but **does not read escape character**. | The readline() also takes input from the user but also **reads the escape character**.                         |
| It has a prompt that represents the default value before the user input.    | Readline has a parameter named size, Which is a non-negative number, it actually defines the bytes to be read. |

## Reference

{% embed url="<https://stackoverflow.com/questions/29454365/what-does-sys-stdin-read>" %}
What does sys.stdin read?
{% endembed %}

{% embed url="<https://www.geeksforgeeks.org/difference-between-input-and-sys-stdin-readline>" %}
Difference between input() and sys.stdin.readline() - GeeksforGeeks
{% endembed %}
