文档解释:

File objects used by the interpreter for standard input, output and errors:

  • stdin is used for all interactive input (including calls to input());

  • stdout is used for the output of print() and expression statements and for the prompts of input();

  • The interpreter’s own prompts and its error messages go to stderr.

1.sys.stdin

sys.stdin是一个标准化输入的方法

input 等价于sys.stdin.readline()

import sysprint('Enter your name: ')
name = sys.stdin.readline()
print('Hello ',name)nickname = input("enter your name: ")
print('Hello',nickname + '\n')
sys.stdin.readline()可以实现标准输入,其中默认输入的格式是字符串,如果是int,float类型则需要强制转换。
try:while 1:print('please input a number: ')n = int(sys.stdin.readline().strip('\n'))print('please input some numbers: ')  # 若是多输入,strip()默认是以空格分割,返回一个包含多个字符串的listsn = sys.stdin.readline().strip()if sn == '':breaksn = list(map(int,sn.split())) # 将列表元素转化成int类型print(n)print(sn,'\n')
except:pass

拓展:map(function,iterable,...)

map() 会根据提供的函数对指定序列做映射。

def square(x):return x ** 2ret = map(square, [1, 2, 3, 4, 5])
print(list(ret))# [1, 4, 9, 16, 25]

2.sys.stdout

stdout用于print和状态表达式的结果输出,及input()的瞬时输出

2.1 python的print 等价于sys.stdout.write()

import ossys.stdout.write("hello world" + "\n")
print("hello world")

2.2 sys.stdout 重定向

import  os
import sys
temp = sys.stdout
f = open('test.txt','w')
print('heyyy')  # 打印到终端# 之后使用print函数,都将内容打印到test.txt 文件中
sys.stdout = f
print('hello')  # 打印到文件中# 恢复print函数打印到终端上
sys.stdout = temp
print('恢复')  # 打印到终端
f.close()

3.sys.stderr

stderr与stdout一样,用于重定向错误信息至某个文件。

import sys
import traceback
__stderr__ = sys.stderr
sys.stderr = open('errorlog_abc.txt','a')# 使用traceback 函数定位错误信息
try:1/0
except:traceback.print_exc()

运行结果如下:

python 中的 sys.stdin ,sys.stdout 和sys.stderr相关推荐

  1. python sys stdout_如何理解python中的sys.stdout和sys.stderr

    我有以下简单的python代码. stdout = sys.stdout stderr = sys.stderr try: # omited finally: sys.stdout = stdout ...

  2. 【python ACM 输入输出的处理:sys.stdin.readline().strip().split())】

    [python & ACM 输入输出的处理:sys.stdin.readline().strip().split())] 一.输入处理 按行读取数据 readline() 会返回一个字符串: ...

  3. pythonstdin_python 笔试输入:sys.stdin.readline和input

    ①:输入一行数据并输 出两种方法 # 输入一行数据并输出 import sys # 方法一: str1 = input() print('input 输入:',str1,'len=',len(str1 ...

  4. qt调用import sys库_Python模块之 sys 模块

    引入 1.什么是 sys 模块 sys 模块是与 Python 解释器交互的一个接口 该模块提供对解释器使用或维护的一些变量的访问和获取 它提供了许多函数和变量来处理 Python 运行时环境的不同部 ...

  5. python中if brthon环境安装包_python-模块系列

    python-模块系列 楚时邀月 2015-12-08 --> 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了 ...

  6. 怎样用python定位别人在哪_如何在Python中获得精确的android gps定位?

    我尝试用Python获取我的Android手机的GPS位置(使用QPython3应用程序)但似乎Android中有几个LocationProviders:gps:纯gps位置,缓慢,耗能,但非常准确, ...

  7. python中字典不自动排序/删除指定类型文件/执行可执行文件的返回值

    1.python 字典的用法 from collections import OrderedDict dict =OrderedDict() dict['foo']=3 dcit['aol']=1 2 ...

  8. Python中 os.popen、os.system和subprocess.popen方法介绍

    Python 提供了多种与操作系统交互的方法,比如os模块中的popen和system方法,此外,Python subprocess模块中的Popen类也提供了与操作系统交互的方法,使用起来更加灵活, ...

  9. python中sys.stdout、sys.stdin

    如果需要更好的控制输出,而print不能满足需求,sys.stdout,sys.stdin,sys.stderr就是你需要的. 1. sys.stdout与print: 在python中调用print ...

最新文章

  1. MVC,MVP 和 MVVM 的图示
  2. 判断CSS与JS是否加载完毕的方法
  3. 重工行业:IT运维精细化管理新篇章
  4. pytorch 卷积
  5. python3 字符串比较_Python3 字符串
  6. 企业与黑客攻击 就是一场军备竞赛
  7. ACL 2021 | 丁香园知识增强预训练模型
  8. Mac启动Apache之后访问localhost提示403 Forbidden
  9. 关于前端模块化你应该知道的
  10. 2022全新趣盒iapp源码带后台非常好看UI
  11. Builder模式和工厂模式的区别
  12. 华三华为设备序列号查看生产日期
  13. 关于2020计算机考研。
  14. unity-3d摄像头
  15. python画代码雨
  16. PHP7.4编译安装
  17. Android Studio 简易计算机源码
  18. 安卓快手跳一跳辅助v1.0.0
  19. CSR8670脱机运行后串口通信异常
  20. Java实现客户信息管理软件

热门文章

  1. keil (v5.24.2.0)、protues8.9、STM32F103R6,点灯试验仿真protues报错unclocked peripheral at 0x40010800
  2. 网站攻防之CSRF和XSS跨站脚本攻击
  3. 面向接口编程的个人理解
  4. 那些有趣的网站系列(四)
  5. linux系统从开机到登陆界面的启动过程
  6. 优化函数和损失函数的区别与联系
  7. swift python混合开发_引用swift项目
  8. Java程序员职业发展规划和方向
  9. Fluke 1550C、FLUKE 1555高压绝缘电阻测试仪
  10. 文本意图识别方案整理