sys.stdin是标准化输入的方法
import sys
print('Plase input your name: ')
name = sys.stdin.readline()
print('Hello ', name)
Plase input your name:
Ming
Hello Ming
python3中使用sys.stdin.readline()可以实现标准输入,其中默认输入的格式是字符串,如果是int,float类型则需要强制转换。
sys.stdin用的较少,一般使用stdout和stderr
sys.stdout是将标准输出重新定向到指定文件
import sys
temp = sys.stdout #将当前默认输出路径保存为temp
sys.stdout = open('log', 'a')
b = print('haha', file=sys.stdout)
运行代码后打印信息不会在屏幕上显示,而是会写到log文件中
sys.stderr是将标准错误信息重定向输出到错误文件中
import sys
temp = sys.stdout #将当前默认输出路径保存为temp
sys.stdout = open('errlog', 'a')
b = print('haha', file=sys.stdout)
运行代码后错误信息不会在屏幕上显示,会写入到errlog文件中
在linux中可以不用指定sys的stdout和stderr文件,可以在linux运行文件时
python test.py >log 2>errlog
这样打印信息会在log文件中,错误信息会展示到errlog文件中
2表示将标准错误信息流,>errlog表示将结果打印到errlog文件中