python script的context环境工具:

Current working directory

os.getcwd

Command-line arguments

sys.argv

Shell variables

os.environ

Standard streams

sys.stdin
sys.stdout
sys.stderr

这些工具被用来输入到脚本或者被当作配置参数。

 

Current working directory:

在执行脚本时,如果不包含完整的路径,则首先搜索当前目录

 

Standard streams

标准流有三种类型:

sys.stdin  接收键盘输入

sys.stdout  输出到屏幕

sys.stderr 输出到屏幕

 

for example

teststream.py

def interact():
print('Hello stream world') # print sends to sys.stdout
while True:
try:
reply = input('Enter a number>') # input reads sys.stdin
except EOFError:
break # raises an except on eof
else: # input given as a string
num = int(reply)
print("%d squared is %d" % (num, num ** 2))
print('Bye')
if __name__ == '__main__':
interact()

 

D:\Python33>python teststream.py
Hello stream world
Enter a number>12
12 squared is 144
Enter a number>100
100 squared is 10000
Enter a number>

 

D:\Python33>type input.txt
1
2
3
4
5
6
7
8
9
10
D:\Python33>python teststream.py < input.txt
Hello stream world
Enter a number>1 squared is 1
Enter a number>2 squared is 4
Enter a number>3 squared is 9
Enter a number>4 squared is 16
Enter a number>5 squared is 25
Enter a number>6 squared is 36
Enter a number>7 squared is 49
Enter a number>8 squared is 64
Enter a number>9 squared is 81
Enter a number>10 squared is 100
Enter a number>Bye

 

python teststream.py < input.txt > output.txt
type output.txt
D:\Python33>type output.txt
Hello stream world
Enter a number>1 squared is 1
Enter a number>2 squared is 4
Enter a number>3 squared is 9
Enter a number>4 squared is 16
Enter a number>5 squared is 25
Enter a number>6 squared is 36
Enter a number>7 squared is 49
Enter a number>8 squared is 64
Enter a number>9 squared is 81
Enter a number>10 squared is 100
Enter a number>Bye

一个python script的输出可以作为另一个python script的输入。

for example 2:

C:\...\PP4E\System\Streams> type writer.py
print("Help! Help! I'm being repressed!")
print(42)
C:\...\PP4E\System\Streams> type reader.py
print('Got this: "%s"' % input())
import sys
data = sys.stdin.readline()[:-1]
print('The meaning of life is', data, int(data) * 2)
C:\...\PP4E\System\Streams> python writer.py
Help! Help! I'm being repressed!
42
C:\...\PP4E\System\Streams> python writer.py | python reader.py
Got this: "Help! Help! I'm being repressed!"
The meaning of life is 42 84

fot example 3:

type sort.py
import sys # or sorted(sys.stdin)
lines = sys.stdin.readlines() # sort stdin input lines,
lines.sort() # send result to stdout
for line in lines: print(line, end='') # for further processing
type adder.py
import sys
sum = 0
while True:
try:
line = input() # or call sys.stdin.readlines()
except EOFError: # or for line in sys.stdin:
break # input strips \n at end
else:
sum += int(line) # was sting.atoi() in 2nd ed
print(sum)
type data.txt
123
000
999
042
python sorter.py < data.txt sort a file
000
042
123
python adder.py < data.txt sum file
1164
type data.txt | python adder.py sum type output
1164
type writer2.py
for data in (123, 0, 999, 42):
print('%03d' % data)
python writer2.py | python sorter.py sort py output
000
042
123
999
writer2.py | sorter.py shorter form
...same output as prior command on Windows...
python writer2.py | python sorter.py | python adder.py
1164
999

for example 4:改进后的代码

type sorterSmall.py
import sys
for line in sorted(sys.stdin): print(line, end='')
type adderSmall.py
import sys
print(sum(int(line) for line in sys.stdin))