天天看點

Sys子產品的使用 | 手把手教你入門Python之七十四

上一篇: 記憶體中寫入資料| 手把手教你入門Python之七十三 下一篇: 序列化和反序列化 | 手把手教你入門Python之七十五 本文來自于千鋒教育在阿裡雲開發者社群學習中心上線課程 《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)  # 錯誤輸出           

接收輸入:

Sys子產品的使用 | 手把手教你入門Python之七十四
import sys


s_in = sys.stdin 

while True:
    content = s_in.readline().rstrip('\n')   # hello\n ==> hello  \n ==> ' '
    if content == ' ':
        break
    print(content)           
Sys子產品的使用 | 手把手教你入門Python之七十四

配套視訊