本文來自於千鋒教育在阿里雲開發者社區學習中心上線課程《Python入門2020最新大課》,主講人姜偉。
Sys模塊的使用
import sys
# sys.stdin # 接收用戶的輸入,也就是讀取鍵盤裡輸入的數據
# stdin和stdout默認都是控制檯
# sys.stdout # 標準輸出
# sys.stderr # 錯誤輸出
s_in = sys.stdin # input就是讀取sys.stdin裡的數據
while True:
content = s_in.readline() # hello\n
if content == '\n':
break
print(content)
sys.stdout = open('stdout.txt', 'w', encoding='utf8')
print('hello') # hello
print('yes')
sys.stderr = open('stderr.txt', 'w', encoding='utf8')
print(1 / 0) # 錯誤輸出
接收輸入:
import sys
s_in = sys.stdin
while True:
content = s_in.readline().rstrip('\n') # hello\n ==> hello \n ==> ' '
if content == ' ':
break
print(content)
接收輸入: