Python Concepts/Console Output

From Wikiversity
Jump to: navigation, search

Objective[edit]

Books-aj.svg aj ashton 01f.png

Page Index[edit]

Lesson[edit]

In the context of this lesson, the word "console" implies the visual display which you see in front of you when you sit at your desk, and also the keyboard. The visual display provides console output and the keyboard provides "console input."


The simplest way to write to the console or visual display is python's print function.

$ cat t4.py
print ('Hello, world!') # Contents of python script t4.py
$ python3.6 t4.py # Execute the python script.
Hello, world! 
$

When the print statement in the script was executed, the string 'Hello, world!' appeared on the visual display in front of us. In technical terms the string was written to standard output, usually the console.

Python reserves two file objects for console output: standard output and standard error:

>>> import sys
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
>>> sys.stderr
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
>>>

Both of the above file objects are always open for writing in text mode. Each of the file objects has its associated file descriptor:

>>> sys.stdout.fileno()
1
>>> sys.stderr.fileno()
2

File descriptor 1 usually means standard output, file descriptor 2 usually means standard error. When you write to stdout or stderr, the data written usually appears on the visual display in front of you unless their outputs are redirected.

Writing to standard output[edit]

$ cat t4.py
import sys
import os

print ('line 1 to stdout  ')
sys.stdout.write('line 2 to stdout  ')
os.write(1, b'line 3 to stdout  ')
$ python3.6 t4.py 
line 1 to stdout  
line 3 to stdout  line 2 to stdout  $

Line 3 was printed before line 2. To produce the expected result flush the buffer as necessary.

import sys
import os

print ('line 1 to stdout  ')
sys.stdout.write('line 2 to stdout  ') ; sys.stdout.flush()
os.write(1, b'line 3 to stdout  ')
$ python3.6 t4.py 
line 1 to stdout  
line 2 to stdout  line 3 to stdout  $

To produce acceptable output add the newline at the end of each line as necessary:

import sys
import os

print ('line 1 to stdout  ')
sys.stdout.write('line 2 to stdout  \n') ; sys.stdout.flush()
os.write(1, b'line 3 to stdout  \n')
$ python3.6 t4.py 
line 1 to stdout  
line 2 to stdout  
line 3 to stdout  
$

Writing to standard error[edit]

import sys
import os

print ('    line 1e to stderr  ', file=sys.stderr)
sys.stderr.write('    line 2e to stderr  \n') ; sys.stderr.flush()
os.write(2, b'    line 3e to stderr  \n')
$ python3.6 t4.py 
    line 1e to stderr
    line 2e to stderr
    line 3e to stderr
$ python3.6 t4.py 2>/dev/null # Standard error redirected.
$                             # No output to console.

Output streams redirected[edit]

import sys
import os

print ('    line 1 to stdout  ')
sys.stdout.write('    line 2 to stdout  \n') ; sys.stdout.flush()
os.write(1, b'    line 3 to stdout  \n')

print ('    line 1e to stderr  ', file=sys.stderr)
sys.stderr.write('    line 2e to stderr  \n') ; sys.stderr.flush()
os.write(2, b'    line 3e to stderr  \n')
$ python3.6 t4.py     # No output redirection
    line 1 to stdout  # All output appears on the console.
    line 2 to stdout  
    line 3 to stdout  
    line 1e to stderr  
    line 2e to stderr  
    line 3e to stderr  
$ python3.6 t4.py 1>/dev/null # stdout redirected
    line 1e to stderr         # Only stderr appears on console.
    line 2e to stderr  
    line 3e to stderr  
$ python3.6 t4.py 2>/dev/null # stderr redirected
    line 1 to stdout          # Only stdout appears on console.
    line 2 to stdout  
    line 3 to stdout  
$ python3.6 t4.py 1>/dev/null 2>/dev/null # Both streams redirected.
$                                         # No output to console.
$
$ python3.6 t4.py >/dev/null # If no fd is specified, default is 1.
    line 1e to stderr  
    line 2e to stderr  
    line 3e to stderr  
$

Assignments[edit]

Crystal Clear app kedit.svg

Further Reading or Review[edit]

Smiley green alien cry.svg Completion status: this resource is a stub, which means that pretty much nothing has been done yet.

References[edit]


1. Python's documentation:


2. Python's methods:

"16.2.3.1. I/O Base Classes," "16.2.3.4. Text I/O"


3. Python's built-in functions:

"os.fstat(fd)," "os.fsync(fd)," "os.write(fd, str)," "os.sync()," "print(*objects, ....)"