Standard Files

There are generally three standard files which are made available to you when your program starts. These are standard input (usually the keyboard), standard output (buffered output to the monitor or display), and standard error (unbuffered output to the screen). (The “buffered” or “unbuffered” output refers to that third argument to open()). These files are named stdin, stdout, and stderr and take after their names from the C language. When we say these files are “available to you when your program starts,” that means that these files are pre-opened for you, and access to these files may commence once you have their file handles.
 
Python makes these file handles available to you from the sys module. Once you import sys, you have access to these files as sys.stdin, sys.stdout, and sys.stderr. The print statement normally outputs to sys.stdout while the raw_input() built-in function receives its input from sys.stdin.
 
We will now take yet another look at the “Hello World!” program so that you can compare the similarities and differences between using print/raw_input() and directly with the file names:

print

print 'Hello World!'

sys.stdout.write()

import sys
sys.stdout.write('Hello World!' + 'n)

Notice that we have to explicitly provide the NEWLINE character to sys.stdout’s write() method. In the input examples below, we do not because readline() executed on sys.stdin preserves the readline. raw_input() does not, hence we will allow print to add its NEWLINE.

raw_input()

aString = raw_input('Enter a string: ')
print aString

sys.stdin.readline()

import sys
sys.stdout.write('Enter a string: ')
aString = sys.stdin.readline()
sys.stdout.write(aString)