天天看点

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之七十四

配套视频