sys stdin and stderr
sys stdin readline
import sys
data = sys.stdin.readline()
print data
sys stdin readlines
Sort stdin input lines and send result to stdout.
import sys
lines = sys.stdin.readlines()
lines.sort()
for line in lines: print line,
stdin.read()
Counts the Words in sys.stdin
import sys# from w ww .ja v a 2s . c o m
text = sys.stdin.read()
words = text.split()
wordcount = len(words)
print 'Wordcount:'
print wordcount
Redirecting Output
sys
module has the stdout and stderr
attributes that point to files
used for standard output and standard error output.
import sys# from ww w. j a v a 2s . c o m
sOUT = sys.stdout
sERR = sys.stderr
sys.stdout = open("ouput.txt", "w")
sys.stderr = sys.stdout
sys.stdout = sOUT
sys.stderr = sERR
Redirecting Output
import sys # ww w .j ava 2 s.c o m
saveout = sys.stdout
fsock = open('out.log', 'w')
sys.stdout = fsock
print 'This message will be logged instead of displayed. java2s.com'
sys.stdout = saveout
fsock.close()
stdout.write and stderr.write
import sys # www . j av a 2s. c om
for i in range(3):
sys.stdout.write('java2s.com')
for i in range(3):
sys.stderr.write('java2s.com')
The code above generates the following result.