Screen Input/Output and Redirection - Page 11
May 30, 2001
The built-in raw_ input method can be use to prompt
for and read an input string.
raw_ input can prompt for and read
in a string.
>>> x = raw_input("enter file name to use:")
enter file name to use: myfile
>>> s
'myfile'
The prompt line is optional and the newline at the end of the
input line is stripped off. We could read in numbers with
raw_ input, obtaining a string version that we
convert.
>>> x = int( raw_input("enter your number:"))
enter your number: 39
>>> x
39
A more general approach is to use another built-in function,
input.
>>> x = input(" enter your number:")
enter your number: 39
>>> x
39
With input we have excellent flexibility as it can
actually read in any valid Python expression. Thus, we can read
in a floating point or complex number or a delimited string.
Anything that is not a valid expression will result in a
syntaxError exception being raised.
inputcan prompt for and read in any
valid
>>> x = input("enter your number:")
enter your number: 47+ 3j
>>> x
(47+ 3j)
>>> x = input()
4 + 10/ 2.0
>>> x
9.0
>>> x = input("enter a delimited string:")
enter a delimited string: 'Here is my delimited string. '
>>> x
'Here is my delimited string. '
>>> input("enter expression:")
enter expression:an undelimited string
Traceback (innermost last):
File "< stdin>", line 1, in
File "< string>", line 1
an undelimited string
^
SyntaxError: invalid syntax
Both raw_ input and input write their
prompt to the standard output and read from the
standard input. Lower level access to these and
standard error can be had using the sys module. It
has sys. stdin, sys. stdout, and sys.
stderr attributes. These can be treated as specialized
file objects.
For sys.stdin we have read, readline,
and readlines methods. For sys.stdout and
sys.stderr there are the write and
writelines methods. These operate as they do for
other file objects.
>>> import sys
>>> sys.stdout.write("Write to the standard output.\n")
Write to the standard output.
>>> s = sys. stdin.readline()
An input line
>>> s
'An input line\012'
We can redirect standard input to read from a file. Similarly,
standard output or standard error can be set to write to files.
They can also be subsequently programmatically restored to their
original values using sys.__ stdin__, sys.__
stdout__, and sys.__ stderr__:
>>> import sys
>>> f=open("outFile.txt", W)
>>> sys.stdout = f
>>> sys.stdout.writelines(["A first line.\n"," A seconde line.\n"])
>>> print "A line from the print statement""
>>> 3+ 4
>>> sys.stdout = sys.__ stdout__
>>> f.close()
>>> 3+ 4
7
While the standard output was redirected, we received prompts
and we would have received any tracebacks from errors, but no
other output. If you are using IDLE, these examples using
sys.__ stdout__ will not work as indicated. You
will have to use the interpreter's interactive mode directly.
This would normally be used when you are running from a script
or program. However, if you are using the interactive mode on
Windows you might want to temporarily redirect standard output
in order to capture what might otherwise scroll off the screen.
The following short module implements a set of functions that
provide this capability. Here, CaptureOutput() will
redirect standard output to a file that defaults to
"captureFile. txt". The function
RestoreOutput() will restore standard output to the
default. Also PrintFile() will print this file to
the standard output and ClearFile() will clear it
of its current contents.
Functions to Read and Write Text or Binary Data - Page 10
The Quick Python Book
Output and Redirection - Page 12
|